Open In App

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 keys in the first array using the specified IComparer;T> generic interface. Here in the 2 arrays one contains the keys and the other contains the corresponding items.
 



Syntax: public static void Sort<TKey, TValue> (TKey[] keys, TValue[] items, IComparer comparer); 
Here, TKey is the type of the elements of the key array and TValue the type of the elements of the items array.
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 the keys in keys. 
comparer: It is the IComparer<T> generic interface implementation to use when comparing elements. 
 

Exceptions:
 



Example:
 




// C# program to demonstrate the use of
// Array.Sort<TKey, TValue>(TKey[],
// TValue[], IComparer<TKey>) Method
using System;
using System.Collections.Generic;
 
class compare : IComparer<string> {
 
    public int Compare(string x, string y)
    {
        // Compare x to y
        return x.CompareTo(y);
    }
}
 
// Driver Class
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Initialize two array
        String[] arr1 = { "H", "J", "K",
                   "L", "I", "N", "M" };
 
        String[] arr2 = { "A", "E", "D",
                   "C", "F", "B", "G" };
 
        // Instantiate the IComparer object
        compare g = new compare();
 
        // Display original values of the array
        Console.WriteLine("The original order of"
                    + " elements in the array:");
 
        Display(arr1, arr2);
 
        // Sort the array
        // "arr1" is keys array
        // "arr2" is items array
        // "g" is IComparer<TKey> 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<TKey, TValue>(TKey[], TValue[], Int32, Int32) Method

This method is used to sort a range of elements in a pair of array objects based on the keys in the first array. Here in the 2 arrays one contains the keys and the other contains the corresponding items.
 

Syntax: public static void Sort<TKey, TValue> (TKey[] keys, TValue[] items, IComparer comparer, int index, int len); 
Here, TKey is the type of the elements of the key array and TValue is the type of the elements of the items array.
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 the keys in keys. 
comparer: It is the IComparer<T> generic interface implementation to use when comparing elements. 
index: It is the starting index of the range to sort. 
len: It is the number of elements in the range to sort. 
 

Exceptions:
 

Example:
 




// C# program to demonstrate the use of
// Array.Sort<TKey, TValue>(TKey[], TValue[],
// Int32, Int32) Method
using System;
 
// Driver Class
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Initialize two array
        String[] arr1 = {"H", "J", "K",
                   "L", "I", "M", "N"};
 
        String[] arr2 = {"A", "E", "D",
                   "C", "F", "B", "G"};
 
        // Display original values of the array
        Console.WriteLine("The original order of"
                    + " elements in the array:");
 
        Display(arr1, arr2);
 
        // Sort the array
        // "arr1" is keys array
        // "arr2" is items array
        // start index 1
        // range upto index 5
        Array.Sort(arr1, arr2, 1, 5);
 
        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
M : B
N : G

After Sorting: 
H : A
I : F
J : E
K : D
L : C
M : B
N : G

 

Sort<TKey, TValue>(TKey[], TValue[], Int32, Int32, IComparer<TKey>) Method

This method is used to sort a range of elements in a pair of array objects based on the keys in the first array using the specified IComparer<T> generic interface. Here in the 2 arrays one contains the keys and the other contains the corresponding items.
 

Syntax: public static void Sort<TKey,TValue> (TKey[] keys, TValue[] items, int index, int len, IComparer<TKey> 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 the keys in keys. 
comparer: It is the IComparer<T> generic interface implementation to use when comparing elements. 
index: It is the starting index of the range to sort. 
len: It is the number of elements in the range to sort. 
comparer: It is the IComparer<T> generic interface implementation to use when comparing elements. 
 

Exceptions:
 

Example:
 




// C# program to demonstrate the use of
// Array.Sort<TKey, TValue>(TKey[], TValue[],
// Int32, Int32, IComparer<TKey>) Method
using System;
using System.Collections.Generic;
 
class compare : IComparer<string> {
 
    public int Compare(string x, string y)
    {
        // Compare x to y
        return x.CompareTo(y);
    }
}
 
// Driver Class
class GFG {
 
    // Main Method
    public static void Main()
    {
        // Initialize two array
        String[] arr1 = {"H", "J", "K",
                   "L", "I", "M", "N"};
 
        String[] arr2 = {"A", "E", "D",
                   "C", "F", "B", "G"};
 
        // Instantiate the IComparer object
        compare g = new compare();
 
        // Display original values of the array
        Console.WriteLine("The original order of"
                    + " elements in the array:");
 
        Display(arr1, arr2);
 
        // Sort the array
        // "arr1" is keys array
        // "arr2" is items array
        // "g" is IComparer<TKey> object
        // start index 1
        // range upto index 5
        Array.Sort(arr1, arr2, 1, 5, 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
M : B
N : G

After Sorting: 
H : A
I : F
J : E
K : D
L : C
M : B
N : G

 

Reference: 
 

 


Article Tags :