csharp List Sort

public static void ListSorter()
{
	List<string> myList = new List<string>();

	myList.Add("String 1");
	myList.Add("String 3");
	myList.Add("String 2");

	foreach (string item in myList)
	{
		Console.WriteLine(item);
	}
	/*
		String 1
		String 3
		String 2
	*/

	myList.Sort();

	foreach (string item in myList)
	{
		Console.WriteLine(item);
	}
	/*
		String 1
		String 2
		String 3
	*/
	
	//sort descending using Linq:
	var desc = from s in myList
			   orderby s descending
			   select s;

	foreach (string item in desc)
	{
		Console.WriteLine(item);
	}
}
Sorts a list alphabetically using both .Sort() extension method of List and also Linq.

Updated: Thursday 11th November 2010, 17:44pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.