Export (0) Print
Expand All
6 out of 24 rated this helpful - Rate this topic

Array.Sort Method

Sorts the elements in one-dimensional Array objects.

Overload List

Sorts the elements in an entire one-dimensional Array using the IComparable interface implemented by each element of the Array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Sub Sort(Array)
[C#] public static void Sort(Array);
[C++] public: static void Sort(Array*);
[JScript] public static function Sort(Array);

Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable interface implemented by each key.

[Visual Basic] Overloads Public Shared Sub Sort(Array, Array)
[C#] public static void Sort(Array, Array);
[C++] public: static void Sort(Array*, Array*);
[JScript] public static function Sort(Array, Array);

Sorts the elements in a one-dimensional Array using the specified IComparer interface.

[Visual Basic] Overloads Public Shared Sub Sort(Array, IComparer)
[C#] public static void Sort(Array, IComparer);
[C++] public: static void Sort(Array*, IComparer*);
[JScript] public static function Sort(Array, IComparer);

Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer interface.

[Visual Basic] Overloads Public Shared Sub Sort(Array, Array, IComparer)
[C#] public static void Sort(Array, Array, IComparer);
[C++] public: static void Sort(Array*, Array*, IComparer*);
[JScript] public static function Sort(Array, Array, IComparer);

Sorts the elements in a section of a one-dimensional Array using the IComparable interface implemented by each element of the Array.

[Visual Basic] Overloads Public Shared Sub Sort(Array, Integer, Integer)
[C#] public static void Sort(Array, int, int);
[C++] public: static void Sort(Array*, int, int);
[JScript] public static function Sort(Array, int, int);

Sorts a section of a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable interface implemented by each key.

[Visual Basic] Overloads Public Shared Sub Sort(Array, Array, Integer, Integer)
[C#] public static void Sort(Array, Array, int, int);
[C++] public: static void Sort(Array*, Array*, int, int);
[JScript] public static function Sort(Array, Array, int, int);

Sorts the elements in a section of a one-dimensional Array using the specified IComparer interface.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Sub Sort(Array, Integer, Integer, IComparer)
[C#] public static void Sort(Array, int, int, IComparer);
[C++] public: static void Sort(Array*, int, int, IComparer*);
[JScript] public static function Sort(Array, int, int, IComparer);

Sorts a section of a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer interface.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Sub Sort(Array, Array, Integer, Integer, IComparer)
[C#] public static void Sort(Array, Array, int, int, IComparer);
[C++] public: static void Sort(Array*, Array*, int, int, IComparer*);
[JScript] public static function Sort(Array, Array, int, int, IComparer);

Example

[Visual Basic, C#, C++] The following example shows how to sort two associated arrays where the first array contains the keys and the second array contains the values. Sorts are done using the default comparer and a custom comparer that reverses the sort order. Note that the result might vary depending on the current CultureInfo.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Sort. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Collections

Public Class SamplesArray

   Public Class myReverserClass
      Implements IComparer

      ' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
      Function Compare(x As [Object], y As [Object]) As Integer _
         Implements IComparer.Compare
         Return New CaseInsensitiveComparer().Compare(y, x)
      End Function 'IComparer.Compare

   End Class 'myReverserClass


   Public Shared Sub Main()

      ' Creates and initializes a new Array and a new custom comparer.
      Dim myKeys As [String]() =  {"red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange"}
      Dim myValues As [String]() =  {"strawberries", "PEARS", "LIMES", "BERRIES", "grapes", "olives", "cantaloupe"}
      Dim myComparer = New myReverserClass()

      ' Displays the values of the Array.
      Console.WriteLine("The Array initially contains the following values:")
      PrintKeysAndValues(myKeys, myValues)

      ' Sorts a section of the Array using the default comparer.
      Array.Sort(myKeys, myValues, 1, 3)
      Console.WriteLine("After sorting a section of the Array using the default comparer:")
      PrintKeysAndValues(myKeys, myValues)

      ' Sorts a section of the Array using the reverse case-insensitive comparer.
      Array.Sort(myKeys, myValues, 1, 3, myComparer)
      Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
      PrintKeysAndValues(myKeys, myValues)

      ' Sorts the entire Array using the default comparer.
      Array.Sort(myKeys, myValues)
      Console.WriteLine("After sorting the entire Array using the default comparer:")
      PrintKeysAndValues(myKeys, myValues)

      ' Sorts the entire Array using the reverse case-insensitive comparer.
      Array.Sort(myKeys, myValues, myComparer)
      Console.WriteLine("After sorting the entire Array using the reverse case-insensitive comparer:")
      PrintKeysAndValues(myKeys, myValues)

   End Sub 'Main


   Public Shared Sub PrintKeysAndValues(myKeys() As [String], myValues() As [String])

      Dim i As Integer
      For i = 0 To myKeys.Length - 1
         Console.WriteLine("   {0,-10}: {1}", myKeys(i), myValues(i))
      Next i
      Console.WriteLine()

   End Sub 'PrintKeysAndValues

End Class 'SamplesArray


'This code produces the following output.
'
'The Array initially contains the following values:
'   red       : strawberries
'   GREEN     : PEARS
'   YELLOW    : LIMES
'   BLUE      : BERRIES
'   purple    : grapes
'   black     : olives
'   orange    : cantaloupe
'
'After sorting a section of the Array using the default comparer:
'   red       : strawberries
'   BLUE      : BERRIES
'   GREEN     : PEARS
'   YELLOW    : LIMES
'   purple    : grapes
'   black     : olives
'   orange    : cantaloupe
'
'After sorting a section of the Array using the reverse case-insensitive comparer:
'   red       : strawberries
'   YELLOW    : LIMES
'   GREEN     : PEARS
'   BLUE      : BERRIES
'   purple    : grapes
'   black     : olives
'   orange    : cantaloupe
'
'After sorting the entire Array using the default comparer:
'   black     : olives
'   BLUE      : BERRIES
'   GREEN     : PEARS
'   orange    : cantaloupe
'   purple    : grapes
'   red       : strawberries
'   YELLOW    : LIMES
'
'After sorting the entire Array using the reverse case-insensitive comparer:
'   YELLOW    : LIMES
'   red       : strawberries
'   purple    : grapes
'   orange    : cantaloupe
'   GREEN     : PEARS
'   BLUE      : BERRIES
'   black     : olives


[C#] 
using System;
using System.Collections;

public class SamplesArray  {
 
   public class myReverserClass : IComparer  {

      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
      int IComparer.Compare( Object x, Object y )  {
          return( (new CaseInsensitiveComparer()).Compare( y, x ) );
      }

   }

   public static void Main()  {
 
      // Creates and initializes a new Array and a new custom comparer.
      String[] myKeys = { "red", "GREEN", "YELLOW", "BLUE", "purple", "black", "orange" };
      String[] myValues = { "strawberries", "PEARS", "LIMES", "BERRIES", "grapes", "olives", "cantaloupe" };
      IComparer myComparer = new myReverserClass();
 
      // Displays the values of the Array.
      Console.WriteLine( "The Array initially contains the following values:" );
      PrintKeysAndValues( myKeys, myValues );
 
      // Sorts a section of the Array using the default comparer.
      Array.Sort( myKeys, myValues, 1, 3 );
      Console.WriteLine( "After sorting a section of the Array using the default comparer:" );
      PrintKeysAndValues( myKeys, myValues );

      // Sorts a section of the Array using the reverse case-insensitive comparer.
      Array.Sort( myKeys, myValues, 1, 3, myComparer );
      Console.WriteLine( "After sorting a section of the Array using the reverse case-insensitive comparer:" );
      PrintKeysAndValues( myKeys, myValues );

      // Sorts the entire Array using the default comparer.
      Array.Sort( myKeys, myValues );
      Console.WriteLine( "After sorting the entire Array using the default comparer:" );
      PrintKeysAndValues( myKeys, myValues );

      // Sorts the entire Array using the reverse case-insensitive comparer.
      Array.Sort( myKeys, myValues, myComparer );
      Console.WriteLine( "After sorting the entire Array using the reverse case-insensitive comparer:" );
      PrintKeysAndValues( myKeys, myValues );

   }
 
   public static void PrintKeysAndValues( String[] myKeys, String[] myValues )  {
      for ( int i = 0; i < myKeys.Length; i++ )  {
         Console.WriteLine( "   {0,-10}: {1}", myKeys[i], myValues[i] );
      }
      Console.WriteLine();
   }
}


/* 
This code produces the following output.

The Array initially contains the following values:
   red       : strawberries
   GREEN     : PEARS
   YELLOW    : LIMES
   BLUE      : BERRIES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting a section of the Array using the default comparer:
   red       : strawberries
   BLUE      : BERRIES
   GREEN     : PEARS
   YELLOW    : LIMES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting a section of the Array using the reverse case-insensitive comparer:
   red       : strawberries
   YELLOW    : LIMES
   GREEN     : PEARS
   BLUE      : BERRIES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting the entire Array using the default comparer:
   black     : olives
   BLUE      : BERRIES
   GREEN     : PEARS
   orange    : cantaloupe
   purple    : grapes
   red       : strawberries
   YELLOW    : LIMES

After sorting the entire Array using the reverse case-insensitive comparer:
   YELLOW    : LIMES
   red       : strawberries
   purple    : grapes
   orange    : cantaloupe
   GREEN     : PEARS
   BLUE      : BERRIES
   black     : olives

*/


[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

public __gc class myReverserClass : public IComparer
{
   // Calls CaseInsensitiveComparer::Compare with the parameters reversed.
   int IComparer::Compare(Object* x, Object* y) {
      return((new CaseInsensitiveComparer())->Compare(y, x));
   }
};

void PrintKeysAndValues(String* myKeys[], String* myValues[])
{
   for (int i = 0; i < myKeys->Length; i++) {
      Console::WriteLine(S" {0, -10}: {1}", myKeys->Item[i], myValues->Item[i]);
   }
   Console::WriteLine();
}

int main()
{
   // Creates and initializes a new Array and a new custom comparer.
   String* myKeys[] = { S"red", S"GREEN", S"YELLOW", S"BLUE", S"purple", S"black", S"orange" };
   String* myValues[] = { S"strawberries", S"PEARS", S"LIMES", S"BERRIES", S"grapes", S"olives", S"cantaloupe" };
   IComparer* myComparer = new myReverserClass();

   // Displays the values of the Array.
   Console::WriteLine(S"The Array initially contains the following values:");
   PrintKeysAndValues(myKeys, myValues);

   // Sorts a section of the Array using the default comparer.
   Array::Sort(myKeys, myValues, 1, 3);
   Console::WriteLine(S"After sorting a section of the Array using the default comparer:");


   // Sorts a section of the Array using the reverse case-insensitive comparer.
   Array::Sort(myKeys, myValues, 1, 3, myComparer);
   Console::WriteLine(S"After sorting a section of the Array using the reverse case-insensitive comparer:");
   PrintKeysAndValues(myKeys, myValues);

   // Sorts the entire Array using the default comparer.
   Array::Sort(myKeys, myValues);
   Console::WriteLine(S"After sorting the entire Array using the default comparer:");
   PrintKeysAndValues(myKeys, myValues);

   // Sorts the entire Array using the reverse case-insensitive comparer.
   Array::Sort(myKeys, myValues, myComparer);
   Console::WriteLine(S"After sorting the entire Array using the reverse case-insensitive comparer:");
   PrintKeysAndValues(myKeys, myValues);

}

/* 
This code produces the following output.

The Array initially contains the following values:
   red       : strawberries
   GREEN     : PEARS
   YELLOW    : LIMES
   BLUE      : BERRIES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting a section of the Array using the default comparer:
   red       : strawberries
   BLUE      : BERRIES
   GREEN     : PEARS
   YELLOW    : LIMES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting a section of the Array using the reverse case-insensitive comparer:
   red       : strawberries
   YELLOW    : LIMES
   GREEN     : PEARS
   BLUE      : BERRIES
   purple    : grapes
   black     : olives
   orange    : cantaloupe

After sorting the entire Array using the default comparer:
   black     : olives
   BLUE      : BERRIES
   GREEN     : PEARS
   orange    : cantaloupe
   purple    : grapes
   red       : strawberries
   YELLOW    : LIMES

After sorting the entire Array using the reverse case-insensitive comparer:
   YELLOW    : LIMES
   red       : strawberries
   purple    : grapes
   orange    : cantaloupe
   GREEN     : PEARS
   BLUE      : BERRIES
   black     : olives

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Array Class | Array Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.