Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
To sort a generic list in C#, you can use the Sort() method. The
Sort() method takes a delegate as its parameter. The delegate is a function that takes two objects of the same type and returns an integer.
The following code sorts a generic list of strings in ascending order:
C#
List<string> names = new List<string>();
names.Add("John");
names.Add("Jane");
names.Add("Peter");
// Sort the list in ascending order.
names.Sort((string s1, string s2) => s1.CompareTo(s2));
In this example, the delegate takes two strings as its parameters and returns an integer. The integer returned by the delegate indicates the order of the two strings. In this case, the delegate compares the two strings lexicographically and returns a negative integer if the first string is less than the second string, a positive integer if the first string is greater than the second string, and 0 if the two strings are equal.
The Sort() method will sort the list in ascending order by default. If you want to sort the list in descending order, you can pass
true as the second parameter to the Sort() method.
Here is the code to sort the list in descending order:
The Sort() method can also be used to sort lists of objects that implement the
IComparable interface. The IComparable interface defines a single method,
CompareTo(), which takes another object of the same type as its parameter and returns an integer. The integer returned by the
CompareTo() method indicates the order of the two objects.
The following code sorts a list of objects that implement the IComparable interface:
C#
class Person : IComparable<Person> {
public int Age { get; set; }
public int CompareTo(Person other) {
return this.Age - other.Age;
}
}
List<Person> people = new List<Person>();
people.Add(new Person { Age = 10 });
people.Add(new Person { Age = 20 });
people.Add(new Person { Age = 30 });
// Sort the list by the Age property.
people.Sort();
In this example, the Person class implements the IComparable interface. The
CompareTo() method compares the ages of the two Person objects and returns the difference.
Liked By
Write Answer
How do I sort a generic list?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
19-Aug-2023To sort a generic list in C#, you can use the
Sort()
method. TheSort()
method takes a delegate as its parameter. The delegate is a function that takes two objects of the same type and returns an integer.The following code sorts a generic list of strings in ascending order:
C#
In this example, the delegate takes two strings as its parameters and returns an integer. The integer returned by the delegate indicates the order of the two strings. In this case, the delegate compares the two strings lexicographically and returns a negative integer if the first string is less than the second string, a positive integer if the first string is greater than the second string, and 0 if the two strings are equal.
The
Sort()
method will sort the list in ascending order by default. If you want to sort the list in descending order, you can passtrue
as the second parameter to theSort()
method.Here is the code to sort the list in descending order:
C#
The
Sort()
method can also be used to sort lists of objects that implement theIComparable
interface. TheIComparable
interface defines a single method,CompareTo()
, which takes another object of the same type as its parameter and returns an integer. The integer returned by theCompareTo()
method indicates the order of the two objects.The following code sorts a list of objects that implement the
IComparable
interface:C#
In this example, the
Person
class implements theIComparable
interface. TheCompareTo()
method compares the ages of the twoPerson
objects and returns the difference.