Sort a enumerable list
Shows how to sort a enumerable list by using a delegate.
Author:
Jonas John
License:
Public domain
Language:
C#
Created:
08/13/2007
Updated:
08/13/2007
Tags:
sorting, example, enumerable, delegates
// Create a simple example list List<string> TestList = new List<string>(); TestList.Add("Venezuela"); TestList.Add("Norway"); TestList.Add("Finland"); TestList.Add("Brazil"); TestList.Add("Germany"); TestList.Add("Australia"); TestList.Add("Fakeland"); // Sort the list by A-Z TestList.Sort(delegate(string A, string B) { return A.CompareTo(B); }); // Print out the test list foreach (string Country in TestList) Console.WriteLine(Country); /* Results: Australia Brazil Finland Germany Norway Venezuela */
Feel free to leave a message:
Add a comment
Where is the Fakeland in the Results
I think i had it previously but only on a List<> with simple types like strings or ints
I'm using List<> with custom object a lot more these days, especially when using LINQ so this sorting delegate syntax get's a little tricky to follow
thanks for posting it man :¬)