Sort list of object by properties c#
27,881
Solution 1
Maybe something like this:
List<Leg> legs = GetLegs()
.OrderBy(o=>o.Day)
.ThenBy(o=>o.Hour)
.ThenBy(o=>o.Min).ToList();
Solution 2
You can write a custom IComparer<Leg>
and pass it to the List<T>.Sort
method.
Alternatively, you can implement IComparable<Leg>
in your class and simply call List<T>.Sort
.
Author by
Engern
Updated on July 09, 2022Comments
-
Engern almost 2 years
I have this class:
public class Leg { public int Day { get; set; } public int Hour { get; set; } public int Min { get; set; } }
I have a function that gets a list of legs, called GetLegs()
List<Leg> legs = GetLegs();
Now I would like to sort this list. So I first have to consider the day, then the hour, and at last the minute. How should I solve this sorting?
Thanks