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:
using System;
class Program {
static void Main() {
int[] arr = { 5, 2, 8, 1 };
Array.Sort(arr);
foreach (int i in arr)
Console.Write(i + " ");
}
}
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:
using System;
using System.Collections.Generic;
class DescComparer : IComparer<int> {
public int Compare(int x, int y) {
return y.CompareTo(x);
}
}
class Program {
static void Main() {
int[] arr = { 5, 2, 8, 1 };
Array.Sort(arr, new DescComparer());
foreach (int i in arr)
Console.Write(i + " ");
}
}
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:
using System;
class Program {
static void Main() {
int[] arr = { 9, 4, 7, 2, 8 };
Array.Sort(arr, 1, 3);
foreach (int i in arr)
Console.Write(i + " ");
}
}
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:
using System;
class Program {
static void Main() {
string[] arr = { "apple", "kiwi", "banana" };
Array.Sort(arr, (a, b) => a.Length.CompareTo(b.Length));
foreach (string s in arr)
Console.Write(s + " ");
}
}
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:
using System;
class Program {
static void Main() {
int[] keys = { 3, 1, 2 };
string[] values = { "Three", "One", "Two" };
Array.Sort(keys, values);
for (int i = 0; i < keys.Length; i++)
Console.WriteLine(keys[i] + " -> " + values[i]);
}
}
Output
1 -> One 2 -> Two 3 -> Three
These commonly used overloads of Array.Sort() are sufficient to handle most sorting scenarios efficiently in C#.