Comparing and sorting Tuples C# with Example
Tuples can be compared based on their elements. As an example, an enumerable whose elements are of type Tuple can be sorted based on comparisons operators defined on a specified element: List> list = new List>(); list.Add(new Tuple(2, "foo")); list.Add(new Tuple(1, "bar")); list.Add(new Tuple(3, "qux")); list.Sort((a, b) => a.Item2.CompareTo(b.Item2)); //sort based on the string element foreach (var element in list) { Console.WriteLine(element); } // Output: // (1, bar) // (2, foo) // (3, qux) Or to reverse the sort use: list.Sort((a, b) => b.Item2.CompareTo(a.Item2));