How to sort a C# List Items

The Sort method of List<T> sorts all items of the List using the QuickSort algorithm.
 
The following code example in Listing 1 sorts a List items and displays both original order and sorted order of the List items.
  1. // List of string  
  2. List<string> authors = new List<string>(5);  
  3. authors.Add("Mahesh Chand");  
  4. authors.Add("Chris Love");  
  5. authors.Add("Allen O'neill");  
  6. authors.Add("Naveen Sharma");  
  7. authors.Add("Mahesh Chand");  
  8. authors.Add("Monica Rathbun");  
  9. authors.Add("David McCarter");  
  10.   
  11. Console.WriteLine("Original List items");  
  12. Console.WriteLine("===============");  
  13. // Print original order  
  14. foreach (string a in authors)  
  15. Console.WriteLine(a);  
  16.   
  17. // Sort list items  
  18. authors.Sort();  
  19.   
  20. Console.WriteLine();  
  21. Console.WriteLine("Sorted List items");  
  22. Console.WriteLine("===============");  
  23. // Print sorted items  
  24. foreach (string a in authors)  
  25. Console.WriteLine(a);  
Listing 1.
The output of Listing 8 looks like Figure 1.