C# Array Sort(T[], Int32, Int32, IComparer)

Description

Array Sort (T[], Int32, Int32, IComparer ) sorts the elements in a range of elements in an Array using the specified IComparer generic interface.

Syntax

Array.Sort<T>(T[], Int32, Int32, IComparer<T>) has the following syntax.


public static void Sort<T>(
  T[] array,/* w  ww. j a  v a 2  s.  com*/
  int index,
  int length,
  IComparer<T> comparer
)

Parameters

Array.Sort<T>(T[], Int32, Int32, IComparer<T>) has the following parameters.

  • T - The type of the elements of the array.
  • array - The one-dimensional, zero-based Array to sort.
  • index - The starting index of the range to sort.
  • length - The number of elements in the range to sort.
  • comparer - The IComparer generic interface implementation to use when comparing elements, or null to use the IComparable generic interface implementation of each element.

Returns

Array.Sort<T>(T[], Int32, Int32, IComparer<T>) method returns

Example


using System;/*from   ww  w  .  ja  v a 2  s  . c o  m*/
using System.Collections.Generic;

public class ReverseComparer: IComparer<string>
{
    public int Compare(string x, string y)
    {
        // Compare y and x in reverse order. 
        return y.CompareTo(x);
    }
}

public class Example
{
    public static void Main()
    {
        string[] dinosaurs = {"2016", 
                              "1999", 
                              "2000", 
                              "1234",
                              "2001", 
                              "2345"};

        Array.Sort(dinosaurs, 3, 3, new ReverseComparer());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version