Instead of having a separate array for each property, create a class combining all these properties. Then create a single array of this class:
// Naive implementation
public class Shape
{
public string Type { get; set; }
public string Color { get; set; }
public double Area { get; set; }
}
This allows you to sort like this:
Shape[] shapes = {
new Shape { Type = "circle", Color = "red", Area = 20 },
new Shape { Type = "triangle", Color = "blue", Area = 5 },
new Shape { Type = "square", Color = "orange", Area = 4 }
};
Array.Sort(shapes, (a, b) => a.Area.CompareTo(b.Area));
A better way to represent the shapes and colors is to use enums:
public enum ShapeType
{
Circle,
Triangle,
Square
}
public enum ShapeColor
{
Red,
Blue,
Green,
Orange,
// ...
}
And to implement the class like this
public class Shape
{
public ShapeType Type { get; set; }
public ShapeColor Color { get; set; }
public double Area { get; set; }
}
A more advanced technique is to use inheritance to create different shapes:
public abstract class Shape
{
public Color Color { get; set; }
public abstract double Area { get; }
}
public class Circle : Shape
{
public double Radius { get; set; }
public override double Area => Radius * Radius * Math.PI;
}
public class Square : Shape
{
public double Length { get; set; }
public override double Area => Length * Length;
}
public class Triangle : Shape
{
public double Height { get; set; }
public double Base{ get; set; }
public override double Area => Height * Base / 2;
}
I also use the Color Struct from the System.Drawing Namespace
Arrays have a fixed size. Using lists allows you to add, replace or remove shapes later. Example:
List<Shape> shapes = new() {
new Circle { Color = Color.Red, Radius = 2.52 },
new Triangle { Color = Color.Blue, Base = 4, Height = 2.5 },
new Square { Color = Color.Orange, Length = 2 }
};
// Add a shape later
shapes.Add(new Circle { Color = Color.Yellow, Radius = 3 });
shapes.Sort((a, b) => a.Area.CompareTo(b.Area));
Types,ColorandAreais. You may consider whether or not you can use a class. If you use a class, see Use the IComparable and IComparer interfaces in Visual CSharp.Sortonly withshapeArea(as the screenshot suggests) this array will be already in a different order and the second call toSortwill not have to modify it. You probably want only the 2nd call.class ShapewithType,ColorandAreaproperties. That allows you to use LINQ and simply doshapes = shapes.OrderBy(x => x.Area).Sort()sorts an array alphabetically by default. You are not using a comparer interface or anything like it to tell the method how to sort or the criteria to do it, so it sorted alphabetically.