I was getting the error "At least one object must implement IComparable" when trying to order data on the Thickness class, but I'm having difficulty in understanding how to implement ordering in this scenario with IComparable. Or, to be honest, what it even is. Here's my class with IComparable implemented, but not sorting. Any guidance would as always be appreciated.
public class ColourMatrix
{
public List<Item> Items { get; set; }
public class Item
{
public Colour Colours { get; set; }
public List<Thickness> Thicknesses { get; set; }
}
public class Colour
{
public string Name { get; set; }
}
public class Thickness : IComparable<Thickness>
{
public int CompareTo(Thickness that)
{
return this.Measurement.CompareTo(that.Measurement);
}
public int Measurement { get; set; }
public int StandardColour { get; set; } = 1;
}
}
And I need to order the data like so.
var orderedItems = published.Items
.OrderBy(n => n.Colours.Name)
.ThenBy(t => t.Thicknesses.Select(x => x.Measurement));
In terms of output this is what it renders as, it's the 4, 2,1,3 that need to be ordered.
| 4 | 2 | 1 | 3 |
red | x | | | |
green | x | | x | x |
blue | x | x | | |
It should read
| 1 | 2 | 3 | 4 |
red | | | | x |
green | x | | x | x |
blue | | x | | x |
Where x = the StandardColour property value
It sounds like you're not trying to order the items - but change the content of the Thicknesses
property so that all the thicknesses are themselves in order. You'd do that on each item separately. For example:
foreach (var item in published.Items)
{
item.Thicknesses = item.Thicknesses.OrderBy(t => t.Measurement).ToList();
}