C# Sort List in ascending and descending order| C# Examples
List stores collection of elements of the same type. types can be inbuilt or custom-type classes.
The list is a data structure that stores the elements in insertion order, Elements are not sorted.
This tutorial explains to Sort a list in ascending and descending order.
C# List Sort in Ascending and descending order example
There are multiple ways to sort the list of elements in ascending and descending order.
- Using the sort method with Icomparer
The sort
method takes a class
which implements IComparer
. This class compares the value with others, returns it
- 0: if both are equal
- -1: if the first is less than the second number
- 1: if the first number is greater than the second number
Here is an example
using System;
using System.Collections.Generic;
// comparer for integer
public class AscendingOrder: IComparer < int > {
public int Compare(int x, int y) {
int result = x.CompareTo(y);
if (result == 0) {
return x.CompareTo(y);
}
return result;
}
}
public class Program {
public static void Main() {
List < int > numbers = new List < int > ();
numbers.Add(20);
numbers.Add(2);
numbers.Add(25);
numbers.Add(4);
Console.WriteLine(" Insertion Order ");
// prints Employee displayed in insertion order
printObj(numbers);
Console.WriteLine(" Ascending Order ");
// prints Employee displayed id in ascending order
// comparer for integer
IComparer < int > comparer = new AscendingOrder();
numbers.Sort(comparer);
printObj(numbers);
}
public static void printObj(List < int > emps) {
foreach(int e in emps) {
Console.WriteLine(e);
}
}
}
Output:
Insertion Order
20
2
25
4
Ascending Order
2
4
20
25
- using the sort method with inline order logic
The sort
method takes two elements, compareTo those elements return an integer.
Here is an example to sort numbers in ascending and descending order
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List < int > numbers = new List < int > ();
numbers.Add(20);
numbers.Add(2);
numbers.Add(25);
numbers.Add(4);
Console.WriteLine("Insertion Order ");
// prints numbers displayed in insertion order
printObj(numbers);
Console.WriteLine("Ascending Order ");
// prints numbers displayed in ascending order
numbers.Sort((first, second) => first.CompareTo(second));
printObj(numbers);
Console.WriteLine("Descending Order ");
// prints numbers displayed in descending order
numbers.Sort((first, second) => second.CompareTo(first));
printObj(numbers);
}
public static void printObj(List < int > emps) {
foreach(int e in emps) {
Console.WriteLine(e);
}
}
}
Output:
Insertion Order
20
2
25
4
Ascending Order
2
4
20
25
Descending Order
25
20
4
2
How to Sort a List by a property in the object
Let’s create an Employee object
public class Employee {
public string name {
get;
set;
}
public int id {
get;
set;
}
public int salary {
get;
set;
}
public Employee(int Id, string Name, int Salary) {
id = Id;
name = Name;
salary = Salary;
}
}
- Create a List of Employee objects and add employee objects to it.
- Sort employees by id in ascending and descending order
//Ascending Order
employees.Sort((first,second) => first.id.CompareTo(second.id));
// Descending Order
employees.Sort((first,second) => second.id.CompareTo(first.id));
Here is a complete example
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
List < Employee > employees = new List < Employee > ();
employees.Add(new Employee(1, "frank ", 5000));
employees.Add(new Employee(11, "John ", 4000));
employees.Add(new Employee(18, "Mark ", 2000));
employees.Add(new Employee(2, "Eric ", 12000));
// prints Employee displayed in insertion order
printObj(employees);
// prints Employee displayed id in ascending order
employees.Sort((first,second) => first.id.CompareTo(second.id));
printObj(employees);
// prints Employee displayed id in descending order
employees.Sort((first,second) => second.id.CompareTo(first.id));
printObj(employees);
}
public static void printObj(List < Employee > emps) {
foreach(Employee e in emps) {
Console.WriteLine("Id = " + e.id + " Name = " + e.name + " salary = " + e.salary);
}
}
}
Output:
Id = 1 Name = frank salary = 5000
Id = 11 Name = John salary = 4000
Id = 18 Name = Mark salary = 2000
Id = 2 Name = Eric salary = 12000
Id = 1 Name = frank salary = 5000
Id = 2 Name = Eric salary = 12000
Id = 11 Name = John salary = 4000
Id = 18 Name = Mark salary = 2000
Id = 18 Name = Mark salary = 2000
Id = 11 Name = John salary = 4000
Id = 2 Name = Eric salary = 12000
Id = 1 Name = frank salary = 5000