Thursday, February 18, 2010

Sort a enumerable list

// Create a simple example list
List TestList = new List();

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
*/

No comments: