C# Array Sort(T[], Comparison)

Description

Array Sort (T[], Comparison ) sorts the elements in an Array using the specified Comparison .

Syntax

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


public static void Sort<T>(
  T[] array,
  Comparison<T> comparison
)

Parameters

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

  • T - The type of the elements of the array.
  • array - The one-dimensional, zero-based Array to sort
  • comparison - The Comparison to use when comparing elements.

Returns

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

Example


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

public class Example
{
    private static int CompareDinosByLength(string x, string y)
    {
        if (x == null)
        {
            if (y == null)
            {
                return 0;
            }
            else
            {
                return -1;
            }
        }
        return -1;
    }

    public static void Main()
    {
        string[] myValues = {
            "2016",
            "1999",
            null,
            null,
            "2000",
            "2002" };
        Array.Sort(myValues, CompareDinosByLength);
    }
}

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