Thursday, August 28, 2008

useful sorting of Lists

slightly in love with ruby/rails sorting
some_list.sort_by{|person| person.age}

I went ahead and made a sort function that only takes a Generic Func as input
here goes:

public static class ListExtender
    public static void Sort<T>(this List<T> list, Func<T, IComparable> func)
        list.Sort((a, b) => func(a).CompareTo(func(b)));

So I can now sort for instance cards like this:

Cards.Sort(card => card.Value )


No comments: