How to sort an array using Array Sort() method
In this tutorial, you will learn how to sort an array in C# using the Array Sort method().
In addition to the list type, there is an array type as a data format for storing multiple data. For data in the list format, there is a List.Sort method, which could be used to sort the data in ascending/descending order according to the purpose. The array type has its own Array.Sort method, which allows sorting by data type without conversion.
How to sort an array in ascending order
It is basically the same as using the List type. You just need to specify the target array as the argument of the Sort method in the Array object.
The following sample program sort an array of strings named students in ascending order:
using System; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an array of strings with 5 elements string[] students = new string[5] { "James", "John", "Roland", "Martin", "Mike" }; //Sort the array Array.Sort(students); //Display the array elements Display(students); } static void Display(string[] array) { foreach (string element in array) { Console.WriteLine("Array element: {0}", element); } } } }
Output:
Array element: James
Array element: John
Array element: Martin
Array element: Mike
Array element: Roland
How to sort an array in descending order
If you want to sort data of array type in descending order, use Array.Reverse() method. One caveat is that you can’t sort in descending order without being aligned in ascending order, and the order of the original data is not preserved, so if you need the original sequence of data, you need to copy it to another object in advance.
The following sample program sort an array of strings named students in descending order:
using System; namespace CSharpExample { class Program { static void Main(string[] args) { // string[] students = new string[5] { "James", "John", "Roland", "Martin", "Mike" }; Console.WriteLine("Sort the array in ascending order:"); Array.Sort(students); Display(students); Console.WriteLine("*********************"); Console.WriteLine("Reverse the array:"); Array.Reverse(students); Display(students); } static void Display(string[] array) { foreach (string element in array) { Console.WriteLine("Array element: {0}", element); } } } }
Output:
Sort the array in ascending order:
Array element: James
Array element: John
Array element: Martin
Array element: Mike
Array element: Roland
*********************
Reverse the array:
Array element: Roland
Array element: Mike
Array element: Martin
Array element: John
Array element: James
Sorting array of strings using Linq
You can also use Linq to sort arrays. Below is an example of sorting an array of strings using Linq:
using System; using System.Linq; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an array of strings with 5 elements string[] students = new string[5] { "Roland", "James", "John", "Martin", "Mike" }; //Sort the array using Linq string[] new_students = students.OrderBy(x => x).ToArray(); //Display the array elements Display(new_students); } static void Display(string[] array) { foreach (string element in array) { Console.WriteLine("Array element: {0}", element); } } } }
Output:
Array element: James
Array element: John
Array element: Martin
Array element: Mike
Array element: Roland
Sorting an array of objects using Linq
The following sample program shows an example of sorting an array of objects using Linq:
using System; using System.Linq; namespace CSharpExample { class Program { static void Main(string[] args) { //Initialize an array Student[] students = new Student[5]; students[0] = new Student { Name = "James", DateOfBirth = new DateTime(1999, 1, 1), }; students[1] = new Student { Name = "Roland", DateOfBirth = new DateTime(2000, 10, 1), }; students[2] = new Student { Name = "Mike", DateOfBirth = new DateTime(1999, 12, 26), }; students[3] = new Student { Name = "John", DateOfBirth = new DateTime(2000, 2, 10), }; students[4] = new Student { Name = "John", DateOfBirth = new DateTime(1999, 1, 19), }; Student[] newStudents = students.OrderBy(x => x.Name).ThenBy(x=>x.DateOfBirth).ToArray(); Display(newStudents); } static void Display(Student[] students) { foreach (Student element in students) { Console.WriteLine("Student name: {0}, Date of Birth: {1}", element.Name, element.DateOfBirth.ToShortDateString()); } } class Student { public string Name { get; set; } public DateTime DateOfBirth { get; set; } } } }
Output:
Student name: James, Date of Birth: 1/1/1999
Student name: John, Date of Birth: 1/19/1999
Student name: John, Date of Birth: 2/10/2000
Student name: Mike, Date of Birth: 12/26/1999
Student name: Roland, Date of Birth: 10/1/2000
Summary
This C# tutorial explained how to sort an array in ascending/descending order in C# using the Array Sort() method.
If you want to learn more about C# programming, here is a list of C# programming tutorials.