asp.net.ph

Array.Sort Method ( Array )

System Namespace   Array Class


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

[ VB ]
<Serializable>
Overloads Public 
Shared Sub Sort ( _
   ByVal array As Array _
)

[ C# ]
[Serializable]
public static void Sort (
   Array array
);

[ C++ ]
[Serializable]
public: static void Sort (
   Array* array
);

[JScript ]
public Serializable
static function Sort (
   array : Array
);

Parameters

array
The one-dimensional Array to sort.

Exceptions


Exception Type Condition
ArgumentNullException array is a null reference ( Nothing in Visual Basic ).
RankException array is multidimensional.
InvalidOperationException The comparer throws an exception.

Remarks

Each element of array must implement the IComparable interface to be capable of comparisons with every other element in array.

If the sort is not successfully completed, the results are undefined.

This method uses the QuickSort algorithm. This is an O ( n log2n ) operation, where n is the number of elements to sort.

Example

The following code example shows how to sort the values in an Array. Note that the result might vary depending on the current CultureInfo.

[ VB ] 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main ( ) 
        
        ' Creates and initializes a new Array.
        Dim myArray As Array = Array.CreateInstance ( GetType ( String ) , 9 ) 
        myArray.SetValue ( "The", 0 ) 
        myArray.SetValue ( "quick", 1 ) 
        myArray.SetValue ( "brown", 2 ) 
        myArray.SetValue ( "fox", 3 ) 
        myArray.SetValue ( "jumped", 4 ) 
        myArray.SetValue ( "over", 5 ) 
        myArray.SetValue ( "the", 6 ) 
        myArray.SetValue ( "lazy", 7 ) 
        myArray.SetValue ( "dog", 8 ) 
        
        ' Displays the values of the Array.
        Response.WriteLine ( "The Array initially contains the " _
           + "following values:" ) 
        PrintIndexAndValues ( myArray ) 
        
        ' Sorts the values of the Array.
        Array.Sort ( myArray ) 
        
        ' Displays the values of the Array.
        Response.WriteLine ( "After sorting:" ) 
        PrintIndexAndValues ( myArray ) 
    End Sub
    
    
    Public Shared Sub PrintIndexAndValues ( myArray As Array ) 
        Dim i As Integer
        For i = myArray.GetLowerBound ( 0 ) To myArray.GetUpperBound ( 0 ) 
            Response.WriteLine ( ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue ( i ) ) 
        Next i
    End Sub
End Class

' This code produces the following output.
' 
' The Array initially contains the following values:
'     [0]:    The
'     [1]:    quick
'     [2]:    brown
'     [3]:    fox
'     [4]:    jumped
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog
' After sorting:
'     [0]:    brown
'     [1]:    dog
'     [2]:    fox
'     [3]:    jumped
'     [4]:    lazy
'     [5]:    over
'     [6]:    quick
'     [7]:    the
'     [8]:    The 

[ C# ] 
using System;
public class SamplesArray  {

   public static void Main ( )  {

      // Creates and initializes a new Array.
      Array myArray=Array.CreateInstance ( typeof ( String ) , 9 );
      myArray.SetValue ( "The", 0 );
      myArray.SetValue ( "quick", 1 );
      myArray.SetValue ( "brown", 2 );
      myArray.SetValue ( "fox", 3 );
      myArray.SetValue ( "jumped", 4 );
      myArray.SetValue ( "over", 5 );
      myArray.SetValue ( "the", 6 );
      myArray.SetValue ( "lazy", 7 );
      myArray.SetValue ( "dog", 8 );

      // Displays the values of the Array.
      Response.WriteLine ( "The Array initially contains the following values:" );
      PrintIndexAndValues ( myArray );

      // Sorts the values of the Array.
      Array.Sort ( myArray );

      // Displays the values of the Array.
      Response.WriteLine ( "After sorting:" );
      PrintIndexAndValues ( myArray );
   }


   public static void PrintIndexAndValues ( Array myArray )  {
      for ( int i = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ ) 
         Response.WriteLine ( "\t[{0}]:\t{1}", i, myArray.GetValue ( i ) );
   }
}
/* 
This code produces the following output.

The Array initially contains the following values:
    [0]:    The
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumped
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
After sorting:
    [0]:    brown
    [1]:    dog
    [2]:    fox
    [3]:    jumped
    [4]:    lazy
    [5]:    over
    [6]:    quick
    [7]:    the
    [8]:    The
*/

[JScript ] 
import System;

// Creates and initializes a new Array.
var myArray : System.Array= System.Array.CreateInstance ( System.String, 9 );
myArray.SetValue ( "The", 0 );
myArray.SetValue ( "quick", 1 );
myArray.SetValue ( "brown", 2 );
myArray.SetValue ( "fox", 3 );
myArray.SetValue ( "jumped", 4 );
myArray.SetValue ( "over", 5 );
myArray.SetValue ( "the", 6 );
myArray.SetValue ( "lazy", 7 );
myArray.SetValue ( "dog", 8 );

// Displays the values of the Array.
Response.WriteLine ( "The Array initially contains the following values:" );
PrintIndexAndValues ( myArray );

// Sorts the values of the Array.
System.Array.Sort ( myArray );

// Displays the values of the Array.
Response.WriteLine ( "After sorting:" );
PrintIndexAndValues ( myArray );
  
function PrintIndexAndValues ( myArray : System.Array )  {
   for ( var i : int = myArray.GetLowerBound ( 0 ); i <= myArray.GetUpperBound ( 0 ); i++ ) 
      Response.WriteLine ( "\t[{0}]:\t{1}", i, myArray.GetValue ( i ) );
}
 /* 
 This code produces the following output.
 
 The Array initially contains the following values:
     [0]:    The
     [1]:    quick
     [2]:    brown
     [3]:    fox
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 After sorting:
     [0]:    brown
     [1]:    dog
     [2]:    fox
     [3]:    jumped
     [4]:    lazy
     [5]:    over
     [6]:    quick
     [7]:    the
     [8]:    The
 */
See Also

Array Members   Array.Sort Overload List   IComparable   BinarySearch Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph