Sort an array of objects by a specific field in C#
This post will discuss how to sort an array of objects by a specific field in C#.
1. Using Enumerable.OrderBy Method
For out-of-place sorting of an array of objects, use the Enumerable.OrderBy() method by LINQ. It sorts the elements of a sequence in ascending order according to a key. For example, the following code sorts an array of Person objects by the age field:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Linq; public class Person { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; var sorted = people.OrderBy(ob => ob.age).ToArray(); Array.ForEach(sorted, Console.WriteLine); } } |
Output:
[y, 21]
[z, 24]
[x, 27]
[z, 30]
This method can be used with .NET 3.5 and above. To sort in descending order instead, use the Enumerable.OrderByDescending method from LINQ.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Linq; public class Person { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; var sorted = people.OrderByDescending(ob => ob.age).ToArray(); Array.ForEach(sorted, Console.WriteLine); } } |
Output:
[z, 30]
[x, 27]
[z, 24]
[y, 21]
To compare objects against multiple fields, call the ThenBy() or ThenByDescending() method. For instance, the following example sorts an array of objects by the name field. For two objects having the same name, the ordering is decided by their age.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Linq; public class Person { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; var sorted = people.OrderBy(x => x.name) .ThenByDescending(x => x.age) .ToArray(); Array.ForEach(sorted, Console.WriteLine); } } |
Output:
[y, 21]
[z, 24]
[x, 27]
[z, 30]
2. Using Array.Sort method
To sort the array in-place, you can use the Array.Sort method, which will perform sorting using a delegate of type Comparison<T> or an instance of IComparer<T>/IComparable<T>.
For example, the following code uses a comparison delegate to provide precise control over the sort order of elements. The solution uses a lambda expression to compare an array of Person objects first by the name field, followed by the age field.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; public class Person { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; Array.Sort(people, (x, y) => x.age.CompareTo(y.age)); Array.ForEach(people, Console.WriteLine); } } |
Output:
[y, 21]
[z, 24]
[x, 27]
[z, 30]
Alternatively, you can provide IComparable<T> implementation for sorting an array with the Array.Sort method. This can be done by having the class implement the IComparable<T> interface and override its CompareTo() method, as the following example illustrates.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; public class Person: IComparable<Person> { public string name; public int age; public Person(string name, int age) { this.name = name; this.age = age; } public int CompareTo(Person value) { return this.age.CompareTo(value.age); } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { Person[] people = { new Person("x", 27), new Person("y", 21), new Person("z", 24), new Person("z", 30) }; Array.Sort(people); Array.ForEach(people, Console.WriteLine); } } |
Output:
[y, 21]
[z, 24]
[x, 27]
[z, 30]
That’s all about sorting an array of objects by a specific field in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)