ILearnable .Net

August 28, 2008

useful sorting of Lists

Filed under: Uncategorized — andreakn @ 14:45

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 )

Yay!

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: