Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Array.Sort() / Array.Reverse()

Array.Sort() / Array.Reverse()

Since: C# 1.0(2002)

The Array.Sort() method sorts an array in ascending order, and Array.Reverse() reverses the order of the elements in an array.

Syntax

Array.Sort(T[] array)

// Sorts a range of elements starting at index for length elements.
Array.Sort(T[] array, int index, int length)

// Reverses the order of all elements in the array (modifies the original array).
Array.Reverse(T[] array)

// Reverses a range of elements starting at index for length elements.
Array.Reverse(T[] array, int index, int length)

Method List

MethodDescription
Array.Sort(T[] array)Sorts the entire array in ascending order using the default comparison (numeric order for numbers, lexicographic order for strings).
Array.Sort(T[] array, int index, int length)Sorts only the specified range of the array in ascending order.
Array.Reverse(T[] array)Reverses the order of all elements in the array.
Array.Reverse(T[] array, int index, int length)Reverses only the specified range of the array.

Sample Code

Use Array.Sort() to sort a numeric array in ascending order, then Array.Reverse() to reverse it into descending order.

Program.cs
using System;

int[] powers = { 78, 92, 45, 88, 61 };
Array.Sort(powers);
Console.WriteLine(string.Join(", ", powers)); // 45, 61, 78, 88, 92

Array.Reverse(powers);
Console.WriteLine(string.Join(", ", powers)); // 92, 88, 78, 61, 45

string[] members = { "Majima Goro", "Kiryu Kazuma", "Akiyama Shun", "Saejima Taiga" };
Array.Sort(members);
Console.WriteLine(string.Join(", ", members));

Run the following command:

dotnet run
45, 61, 78, 88, 92
92, 88, 78, 61, 45
Akiyama Shun, Kiryu Kazuma, Majima Goro, Saejima Taiga

Sorting a Specific Range

You can specify the range to sort or reverse using the second and third arguments (start index and element count). Elements outside the specified range are not affected.

RangeSortSample.cs
using System;

int[] data = { 10, 50, 30, 20, 40 };
Array.Sort(data, 1, 3); // Sort only 3 elements starting at index 1 (50, 30, 20)
Console.WriteLine(string.Join(", ", data)); // 10, 20, 30, 50, 40

int[] arr = { 1, 2, 3, 4, 5 };
Array.Reverse(arr, 1, 3); // Reverse only 3 elements starting at index 1 (2, 3, 4)
Console.WriteLine(string.Join(", ", arr)); // 1, 4, 3, 2, 5

Run the following command:

dotnet run
10, 20, 30, 50, 40
1, 4, 3, 2, 5

Custom Sort with a Comparison Delegate

Passing a Comparison<T> delegate (lambda expression) to Array.Sort() lets you sort by a custom condition. A negative return value means x comes first; a positive return value means y comes first.

CustomSortSample.cs
using System;

string[] names = { "Kiryu Kazuma", "Majima Goro", "Akiyama Shun", "Saejima Taiga", "Kasuga Ichiban" };

// Sort by string length in ascending order
Array.Sort(names, (x, y) => x.Length - y.Length);
Console.WriteLine(string.Join(", ", names));

// Sort by string length in descending order
Array.Sort(names, (x, y) => y.Length - x.Length);
Console.WriteLine(string.Join(", ", names));

Run the following command:

dotnet run
Majima Goro, Kiryu Kazuma, Akiyama Shun, Saejima Taiga, Kasuga Ichiban
Kasuga Ichiban, Saejima Taiga, Kiryu Kazuma, Akiyama Shun, Majima Goro

Common Mistakes

Common Mistake: Forgetting That the Original Array Is Modified

Both Array.Sort() and Array.Reverse() modify the original array in place. If you need to preserve the state before sorting, clone the array first using Clone() and operate on the copy.

using System;

// NG: the original array is modified and cannot be restored
int[] original = { 3, 1, 4, 1, 5 };
Array.Sort(original);
Console.WriteLine(string.Join(", ", original)); // 1, 1, 3, 4, 5 (no going back)

Run the following command:

dotnet run
1, 1, 3, 4, 5

The following example demonstrates this:

using System;

// OK: clone the array first, then sort the copy
int[] original = { 3, 1, 4, 1, 5 };
int[] sorted = (int[])original.Clone();
Array.Sort(sorted);
Console.WriteLine(string.Join(", ", original)); // 3, 1, 4, 1, 5 (unchanged)
Console.WriteLine(string.Join(", ", sorted)); // 1, 1, 3, 4, 5

Run the following command:

dotnet run
3, 1, 4, 1, 5
1, 1, 3, 4, 5

Notes

Both Array.Sort() and Array.Reverse() modify the original array in place. If you need to preserve the original array, create a copy first using Array.Copy() or Clone() before sorting or reversing.

If you need a custom sort order (for example, sorting objects by a specific property), you can pass a lambda expression to Array.Sort(array, Comparison<T> comparison).

For copying arrays, see Array.IndexOf() / Array.Copy().

If you find any errors or copyright issues, please .