How to sort an Array in C# | Array.Sort() Method Set – 3
Last Updated :
12 Mar, 2019
Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods:
- Sort(Array, IComparer) Method
- Sort(Array, Array, IComparer) Method
- Sort(Array, Array) Method
Sort(Array, IComparer) Method
This method sorts the elements in a one-dimensional array using a specified
IComparer.
Syntax: public static void Sort (Array arr, IComparer comparer);
Parameters:
arr: It is the one-dimensional array to sort.
comparer: It is the implementation to use when comparing elements.
Exceptions:
- ArgumentNullException: If the array arr is null.
- RankException: If the array arr is multidimensional.
- InvalidOperationException: If the comparer is null.
- ArgumentException: If the implementation of comparer caused an error during the sort.
Example 1:
csharp
// C# program to demonstrate the
// Array.Sort(Array, IComparer) method
using System;
using System.Collections;
class compare : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(x, y);
}
}
class GFG {
// Main Method
public static void Main()
{
// Initializing array.
String[] arr = {"A", "D", "B",
"E", "C", "F", "G"};
// Instantiate the IComparer object
IComparer cmp = new compare();
// Display the original values of the array
Console.WriteLine("The Original array:");
display(arr);
// Sort the entire array by using
// the IComparer object
// "cmp" is the IComparer object
Array.Sort(arr, cmp);
Console.WriteLine("\nAfter sorting the array"+
" using the IComparer:");
display(arr);
}
// display function
public static void display(String[] arr)
{
foreach(String a in arr)
Console.WriteLine(a);
}
}
Output:
The Original array:
A
D
B
E
C
F
G
After sorting the array using the IComparer:
A
B
C
D
E
F
G
Example 2:
csharp
// C# program to demonstrate the
// Array.Sort(Array, IComparer) method
using System;
using System.Collections;
class compare : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(x, y);
}
}
class GFG {
// Main Method
public static void Main()
{
// Initializing array.
int[] arr = {10, 1, 9, 8, 3,
4, 6, 5, 2, 7};
// Instantiate the IComparer object
IComparer cmp = new compare();
// Display the original values of the array
Console.WriteLine("The Original array:");
display(arr);
// Sort the entire array by
// using the IComparer object
// "cmp" is the IComparer object
Array.Sort(arr, cmp);
Console.WriteLine("\n\nAfter sorting the "+
"array using the IComparer:");
display(arr);
}
// display function
public static void display(int[] arr)
{
foreach(int a in arr)
Console.Write(a + " ");
}
}
Output:
The Original array:
10 1 9 8 3 4 6 5 2 7
After sorting the array using the IComparer:
1 2 3 4 5 6 7 8 9 10
Sort(Array, Array, IComparer) Method
This method sorts a pair of one-dimensional array objects based on the
keys in the first array using the specified
IComparer.
Syntax: public static void Sort (Array keys, Array items, IComparer comparer);
Parameters:
keys: It is the one-dimensional array that contains the keys to sort.
items: It is the one-dimensional array that contains the items that correspond to each of the keys in the keys array.
comparer: It is the IComparer implementation to use when comparing elements.
Exceptions:
- ArgumentNullException: If the keys is null.
- RankException: If the keys array is multidimensional or the items array is multidimensional.
- ArgumentException: If the items is not null and the length of keys is greater than the length of items or the implementation of comparer caused an error during the sort.
- InvalidOperationException: If the comparer is null.
Example:
csharp
// C# program to demonstrate the
// Array.Sort(Array, Array,
// IComparer) method
using System;
using System.Collections;
class comparer : IComparer {
// Call CaseInsensitiveComparer.Compare
public int Compare(Object x, Object y)
{
return (new CaseInsensitiveComparer()).Compare(x, y);
}
}
class GFG {
// Main Method
public static void Main()
{
// initialize two arrays
String[] arr1 = {"H", "J", "K",
"L", "I", "N", "M"};
String[] arr2 = {"A", "E", "D",
"C", "F", "B", "G"};
// Instantiate the IComparer object
IComparer g = new comparer();
// Display original values of the array.
Console.WriteLine("The original order of "+
"elements in the array:");
Display(arr1, arr2);
// Sort the array using IComparer
// object. "g" is IComparer object
Array.Sort(arr1, arr2, g);
Console.WriteLine("\nAfter sorting :");
Display(arr1, arr2);
}
// Display function
public static void Display(String[] arr1, String[] arr2)
{
for (int i = 0; i < arr1.Length; i++) {
Console.WriteLine(arr1[i] + " : " + arr2[i]);
}
}
}
Output:
The original order of elements in the array:
H : A
J : E
K : D
L : C
I : F
N : B
M : G
After sorting :
H : A
I : F
J : E
K : D
L : C
M : G
N : B
Sort(Array, Array) Method
This method sorts a pair of one-dimensional array objects based on the
keys in the first Array using the
IComparable implementation of each key. Here, in the objects there are two array in which one contains the keys and the other contains the corresponding items.
Syntax: public static void Sort (Array keys, Array items);
Parameters:
keys: It is the one-dimensional array that contains the keys to sort.
items: It is the one-dimensional array that contains the items that correspond to each of the keys in the keys array.
Exceptions:
- ArgumentNullException: If the keys is null.
- RankException: If the keys array is multidimensional or the items array is multidimensional.
- ArgumentException: If the items is not null and the length of keys is greater than the length of items.
- InvalidOperationException: If the one or more elements in the keys array do not implement the IComparable interface.
Example:
csharp
// C# program to demonstrate the
// Array.Sort(Array, Array) method
using System;
class GFG {
// Main Method
public static void Main()
{
// initialize two array.
int[] arr1 = {7, 5, 2, 3, 1, 6, 4};
string[] arr2 = {"A", "E", "D",
"C", "F", "B", "G"};
// Display original values of the array.
Console.WriteLine("The original array:");
Display(arr1, arr2);
// Sort the array using two array
// "arr1" is keys array
// "arr2" item array
Array.Sort(arr1, arr2);
Console.WriteLine("\nAfter Sorting :");
Display(arr1, arr2);
}
// Display function
public static void Display(int[] arr1, string[] arr2)
{
for (int i = 0; i < arr1.Length; i++)
{
Console.WriteLine(arr1[i] + " : " + arr2[i]);
}
}
}
Output:
The original array:
7 : A
5 : E
2 : D
3 : C
1 : F
6 : B
4 : G
After Sorting :
1 : F
2 : D
3 : C
4 : G
5 : E
6 : B
7 : A
Reference:
Similar Reads
How to sort an Array in C# | Array.Sort() Method Set â 2 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, Int32, Int32, IComparer) Method Sort(Array, Array, Int32, Int32, IComparer) Method Sort(Array, Int32, Int32) Method
9 min read
How to Sort an Array in C# | Array.Sort() Method Set - 1 Array.Sort Method in C# is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method as follows:Sort<T>(T[]) MethodSort<T>(T[], IComparer<T>) MethodSort<T>(T[], Int32, Int32) MethodSort<T>(T[], Comparison<T>) Method
8 min read
How to sort an Array in C# | Array.Sort() Method | Set â 4 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array) Method This method sorts the elements in an entire one-dimensional array using the IComparable implementation of ea
5 min read
How to sort an Array in C# | Array.Sort() Method | Set â 5 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort<TKey, TValue>(TKey[], TValue[], IComparer<TKey>) Method This method sorts a pair of array objects based on the
7 min read
How to sort a list in C# | List.Sort() Method Set -2 List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of th
5 min read
How to sort a list in C# | List.Sort() Method Set -1 List<T>.Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements. There are total 4 methods in the overload list of th
5 min read
How to use Array.BinarySearch() Method in C# | Set -2 Array.BinarySearch() method is used to search a value in a sorted one dimensional array. The binary search algorithm is used by this method. This algorithm searches a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the
12 min read
C# | Copying the SortedList elements to an Array Object SortedList.CopyTo(Array, Int32) Method is used to copy SortedList elements to a one-dimensional Array object, starting at the specified index in the array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array object that is the destinat
2 min read
How to find the rank of an array in C# Array.Rank Property is used to get the rank of the Array. Rank is the number of dimensions of an array. For example, 1-D array returns 1, a 2-D array returns 2, and so on. Syntax: public int Rank { get; } Property Value: It returns the rank (number of dimensions) of the Array of type System.Int32. B
2 min read
C# | How to get a subset in a SortedSet SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.GetViewBetween(T, T) method is used to return a view of a subset in a SortedSet<T>. Properties: In C#, SortedSet class can be used to store, remov
2 min read