VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/how-to-sort-an-array-in-c-sharp-array-sort-method-set-1/

⇱ How to Sort an Array in C# using Array.Sort() - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Sort an Array in C# using Array.Sort()

Last Updated : 25 Apr, 2026

Sorting is the process of arranging data in a specific order, typically ascending or descending. It helps in organizing data, making it easier to search, analyze, and process efficiently. In C#, arrays can be sorted using the Array.Sort() method.

  • This method is used to sort elements of a one-dimensional array and provides multiple overloaded methods.
  • It handles different sorting scenarios such as default sorting, custom comparison, and sorting a specific range of elements.

In this article, we will focus on the most commonly used overloads of the Array.Sort() method.

1. Sort<T>(T[]) Method

This method sorts the elements of an array using the default comparison defined by the IComparable<T> interface.

Syntax:

Array.Sort<T>(T[] array);

  • T: Represents the type of elements in the array
  • array: The one-dimensional array of type T to be sorted

Example:


Output
1 2 5 8 

2. Sort<T>(T[], IComparer<T>) Method

This method sorts the elements of an array using a custom comparison logic provided by the IComparer<T> interface.

Syntax:

Array.Sort<T>(T[] array, IComparer<T> comparer);

  • comparer: An object that defines custom comparison logic

Example:


Output
8 5 2 1 

3. Sort<T>(T[], int index, int length) Method

This method sorts a specified range of elements in the array.

Syntax:

Array.Sort<T>(T[] array, int index, int length);

  • index: The starting position from where sorting begins
  • length: The number of elements to sort

Example:


Output
9 2 4 7 8 

4. Sort<T>(T[], Comparison<T>) Method

This method sorts the array using a delegate (Comparison<T>) that defines custom comparison logic.

Syntax:

Array.Sort<T>(T[] array, Comparison<T> comparison);

  • comparison: A delegate that defines custom comparison logic between elements

Example:


Output
kiwi apple banana 

5. Sort(Array, Array) Method

This method sorts one array (keys) and rearranges the corresponding elements in another array (values).

Syntax:

Array.Sort(Array keys, Array items);

  • keys: The array containing elements to be sorted
  • items: The array whose elements are rearranged according to the sorted keys

Example:


Output
1 -> One
2 -> Two
3 -> Three

These commonly used overloads of Array.Sort() are sufficient to handle most sorting scenarios efficiently in C#.

Comment

Explore