How to use the List.Sort function in C# to sort a list
In the C# programming language, we often need to sort the list. The Sort function of the List class is a powerful tool designed for this purpose. This article will introduce how to use the List.Sort function in C# to sort a list, and provide specific code examples to help readers better understand and apply this function.
The List.Sort function is a member function of the List class, used to sort elements in the list. This function accepts an optional Comparison parameter, which is used to specify the sorting method. You can also use the default sorting rules to sort the elements in the list.
The following is a sample code for using the List.Sort function to sort a list of integers:
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int>() { 5, 2, 8, 3, 1 }; Console.WriteLine("排序前的列表:"); foreach (var number in numbers) { Console.Write(number + " "); } Console.WriteLine(); numbers.Sort(); Console.WriteLine("排序后的列表:"); foreach (var number in numbers) { Console.Write(number + " "); } Console.WriteLine(); } }
In the above example, we first create an integer list numbers, which contains 5 unordered integers. . Then use the List.Sort function to sort the list. Finally, we output the contents of the list before sorting and after sorting respectively.
Run the above code, the output result is as follows:
排序前的列表: 5 2 8 3 1 排序后的列表: 1 2 3 5 8
As can be seen from the output result, the elements in the list are sorted in ascending order.
If we want to sort the elements in the list in descending order, we can use the Comparison parameter and pass a custom comparison function. The following is a sample code to implement descending sorting through a custom comparison function:
using System; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int>() { 5, 2, 8, 3, 1 }; Console.WriteLine("排序前的列表:"); foreach (var number in numbers) { Console.Write(number + " "); } Console.WriteLine(); numbers.Sort((x, y) => y.CompareTo(x)); Console.WriteLine("排序后的列表:"); foreach (var number in numbers) { Console.Write(number + " "); } Console.WriteLine(); } }
In the above example, we use the numbers.Sort() function to sort the list, and a lambda expression is passed in internally. (x, y) => y.CompareTo(x)
as the Comparison parameter. This lambda expression specifies a custom comparison method that sorts the elements in the list in descending order.
Run the above code again, the output result is as follows:
排序前的列表: 5 2 8 3 1 排序后的列表: 8 5 3 2 1
As can be seen from the output result, the elements in the list are sorted in descending order.
Summary:
This article introduces how to use the List.Sort function in C# to sort a list. When we need to sort the elements in the list, we can use the List.Sort function and choose the default sorting rule or a custom comparison function as needed to flexibly implement ascending or descending sorting. I hope that readers can become more proficient in using the List.Sort function to sort lists through the introduction and sample code of this article.
The above is the detailed content of How to sort a list using List.Sort function in C#. For more information, please follow other related articles on the PHP Chinese website!