The Sort() method takes a delegate as its parameter. The delegate is a function that takes two elements of the same type and returns an integer value indicating their relative order. The following code sorts a list of integers in ascending order:
C#
List<int> numbers = new List<int>();
numbers.Add(10);
numbers.Add(5);
numbers.Add(20);
numbers.Sort(delegate(int x, int y) {
return x.CompareTo(y);
});
In this code, the delegate is an anonymous method that compares two integers. The
CompareTo() method returns an integer value indicating the relative order of the two integers. A value of -1 means that the first integer is less than the second integer, a value of 0 means that the two integers are equal, and a value of 1 means that the first integer is greater than the second integer.
Using the OrderBy() method from LINQ
The OrderBy() method from LINQ is a more concise way to sort a list. The
OrderBy() method takes a lambda expression as its parameter. The lambda expression specifies the property that the list should be sorted by. The following code sorts the same list of integers as the previous example using the
OrderBy() method:
C#
var sortedNumbers = numbers.OrderBy(x => x);
In this code, the lambda expression specifies that the list should be sorted by the
x property of the int objects.
Which method you use to sort a list depends on your preference. The Sort() method is more flexible, as you can use it to sort a list by any property. However, the
OrderBy() method is more concise and easier to read.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
There are two ways to sort a list in C#:
Sort()method.OrderBy()method from LINQ.Using the
Sort()methodThe
Sort()method takes a delegate as its parameter. The delegate is a function that takes two elements of the same type and returns an integer value indicating their relative order. The following code sorts a list of integers in ascending order:C#
In this code, the delegate is an anonymous method that compares two integers. The
CompareTo()method returns an integer value indicating the relative order of the two integers. A value of -1 means that the first integer is less than the second integer, a value of 0 means that the two integers are equal, and a value of 1 means that the first integer is greater than the second integer.Using the
OrderBy()method from LINQThe
OrderBy()method from LINQ is a more concise way to sort a list. TheOrderBy()method takes a lambda expression as its parameter. The lambda expression specifies the property that the list should be sorted by. The following code sorts the same list of integers as the previous example using theOrderBy()method:C#
In this code, the lambda expression specifies that the list should be sorted by the
xproperty of theintobjects.Which method you use to sort a list depends on your preference. The
Sort()method is more flexible, as you can use it to sort a list by any property. However, theOrderBy()method is more concise and easier to read.