C# Sort Array with Examples

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:

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. //Initialize an array of strings with 5 elements
  9. string[] students = new string[5] {
  10. "James", "John", "Roland", "Martin", "Mike"
  11. };
  12. //Sort the array
  13. Array.Sort(students);
  14. //Display the array elements
  15. Display(students);
  16. }
  17. static void Display(string[] array)
  18. {
  19. foreach (string element in array)
  20. {
  21. Console.WriteLine("Array element: {0}", element);
  22. }
  23. }
  24. }
  25. }

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:

  1. using System;
  2. namespace CSharpExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. //
  9. string[] students = new string[5] {
  10. "James", "John", "Roland", "Martin", "Mike"
  11. };
  12. Console.WriteLine("Sort the array in ascending order:");
  13. Array.Sort(students);
  14. Display(students);
  15. Console.WriteLine("*********************");
  16. Console.WriteLine("Reverse the array:");
  17. Array.Reverse(students);
  18. Display(students);
  19. }
  20. static void Display(string[] array)
  21. {
  22. foreach (string element in array)
  23. {
  24. Console.WriteLine("Array element: {0}", element);
  25. }
  26. }
  27. }
  28. }

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:

  1. using System;
  2. using System.Linq;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an array of strings with 5 elements
  10. string[] students = new string[5] {
  11. "Roland", "James", "John", "Martin", "Mike"
  12. };
  13. //Sort the array using Linq
  14. string[] new_students = students.OrderBy(x => x).ToArray();
  15. //Display the array elements
  16. Display(new_students);
  17. }
  18. static void Display(string[] array)
  19. {
  20. foreach (string element in array)
  21. {
  22. Console.WriteLine("Array element: {0}", element);
  23. }
  24. }
  25. }
  26. }
  27.  

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:

  1. using System;
  2. using System.Linq;
  3. namespace CSharpExample
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Initialize an array
  10. Student[] students = new Student[5];
  11. students[0] = new Student
  12. {
  13. Name = "James",
  14. DateOfBirth = new DateTime(1999, 1, 1),
  15. };
  16. students[1] = new Student
  17. {
  18. Name = "Roland",
  19. DateOfBirth = new DateTime(2000, 10, 1),
  20. };
  21. students[2] = new Student
  22. {
  23. Name = "Mike",
  24. DateOfBirth = new DateTime(1999, 12, 26),
  25. };
  26. students[3] = new Student
  27. {
  28. Name = "John",
  29. DateOfBirth = new DateTime(2000, 2, 10),
  30. };
  31. students[4] = new Student
  32. {
  33. Name = "John",
  34. DateOfBirth = new DateTime(1999, 1, 19),
  35. };
  36. Student[] newStudents = students.OrderBy(x => x.Name).ThenBy(x=>x.DateOfBirth).ToArray();
  37. Display(newStudents);
  38.  
  39. }
  40. static void Display(Student[] students)
  41. {
  42. foreach (Student element in students)
  43. {
  44. Console.WriteLine("Student name: {0}, Date of Birth: {1}", element.Name, element.DateOfBirth.ToShortDateString());
  45. }
  46. }
  47. class Student
  48. {
  49. public string Name { get; set; }
  50. public DateTime DateOfBirth { get; set; }
  51. }
  52. }
  53. }

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.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables