What is the method for sorting a list in C#?

In C#, you can use the List.Sort() method to sort a list. This method takes a comparer as a parameter to determine the order of elements. You can use a lambda expression or a custom comparer to specify the sorting rule. Below is an example code:

List<int> numbers = new List<int>{ 4, 2, 6, 1, 5 };
numbers.Sort((a, b) => a.CompareTo(b));

foreach (int number in numbers)
{
    Console.WriteLine(number);
}

The above code will sort the elements in the numbers list in ascending order and output the result.

bannerAds