7

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

Engern
  • 875
  • 3
  • 11
  • 20
  • 4
    Why don't you use the native `DateTime` struct? Comparison and therefore sorting supported out of the box. See http://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx – Călin Darie Mar 15 '12 at 08:39
  • 1
    By the way - look at the related questions to the right - there's lots of similar threads, you need to learn to search a bit. One for example (first one I looked at)... http://stackoverflow.com/questions/3309188/c-net-how-to-sort-a-list-t-by-a-property-in-the-object – SkonJeet Mar 15 '12 at 08:54
  • As you see, it's my first post here, so I will probably get more used to how things work ;) Thanks for the reply anyway, I can't use DateTime in this scenario unfortunately. – Engern Mar 15 '12 at 15:19

5 Answers5

10

Maybe something like this:

List<Leg> legs = GetLegs()
                .OrderBy(o=>o.Day)
                .ThenBy(o=>o.Hour)
                .ThenBy(o=>o.Min).ToList();
Arion
  • 31,011
  • 10
  • 70
  • 88
1

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.

Matthias
  • 12,053
  • 4
  • 49
  • 91
  • No need to do even that, he can just write an anonymous lambda in `List.Sort`. – Moo-Juice Mar 15 '12 at 08:41
  • 2
    For reasons of code reuse, implementing `IComparable` can be the better alternative if sorting / comparing should happen at more than one location. – Matthias Mar 15 '12 at 08:44
1

You need to implement the IComparable<T> interface on your class to allow a more intuitive way for the objects to be sorted in the C# language. When a class implements IComparable, you must also implement the public method CompareTo(T).

Leg class implements IComparable<Leg>, which means an Leg instance can be compared with other Leg instances.

    #region "Leg Class that implements IComparable interface"
    public class Leg:IComparable<Leg>
    {
        public int Day { get; set; }
        public int Hour { get; set; }
        public int Min { get; set; }

        public int CompareTo(Leg leg)
        {
            if (this.Day == leg.Day)
            {
                if (this.Hour == leg.Hour)
                {
                    return this.Min.CompareTo(leg.Min);
                }
            }
            return this.Day.CompareTo(leg.Day);
        }
    }
    #endregion


   //Main code
   List<Leg> legs = GetLegs();
   legs.Sort();
0

Use Enumerable.OrderBy Method.

Zabavsky
  • 13,340
  • 8
  • 54
  • 79
0

I guess this would help.

var o = legs.OrderBy(x => x.Day)
            .ThenBy(x => x.Hour)
            .ThenBy(x => x.Min);