This example shows how to do a ascending and descending sort of list with Linq:
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
static void Main(string[] args) { List<string> alphabets = new List<string> { "F","C","E","X","D","W" }; Console.WriteLine("Alphabets before sorting "); foreach(string alphabet in alphabets) { Console.Write(alphabet+ " "); } Console.WriteLine(); Console.WriteLine(); List<string> listResult = alphabets.OrderByDescending(item => item).ToList(); List<string> listResult2 = alphabets.OrderBy(item => item).ToList(); Console.WriteLine("Alphabets in descending order "); foreach (string lr in listResult) { Console.Write(lr + " "); } Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Alphabets in ascending order "); foreach (string lr in listResult2) { Console.Write(lr + " "); } Console.ReadKey(); } |
Output: