![]() |
VOOZH | about |
Given a list of integers, now our task is to sort the given list of integers. So we use the OrderBy() method of LINQ. This method is used to sort the elements in the collection in ascending order. This method is overloaded in two different ways:
Example:
Input : [90, 87, 34, 23, 22, 56, 21, 89] Output : [21, 22, 23, 34, 56, 87, 89, 90] Input : [10, 11, 2, 3, 18, 5,] Output : [2, 3, 5, 10, 11, 18]
Approach:
1. Create and initialize a list of integer types. For example nums.
2. Sorting the list(named nums) using OrderBy() method
var result_set = nums.OrderBy(num => num);
3. Display the result using the foreach loop.
Example:
Sorted in Ascending order: 20 33 40 50 60 70