Array.Sort Method


Array.Sort Method

Class

System.Array

Syntax

     Array.Sort(array[, comparer])     Array.Sort(array, index, length[, comparer])     Array.Sort(keys, items[, comparer])     Array.Sort(keys, items, index, length[, comparer]) 


array (required in syntax 1 and 2; any array)

The array of objects to be sorted.


keys (required in syntax 3 and 4; any array)

The array of keys to use for sorting. This array is also sorted.


items (required in syntax 3 and 4; any array)

A parallel array of values to be sorted in the order of keys, their corresponding keys.


index (required in syntax 2 and 4; Integer)

The index at which to start the sort.


length (required in syntax 2 and 4; Integer)

The number of items to include in the sort.


comparer (optional; IComparer interface)

An object implementing the IComparer interface to be used for sorting. If omitted, then the sort uses the IComparable implementation of each element (either from array or keys).

Description

The Sort method sorts a portion of, or all of, an entire one-dimensional array, with an optionally specified key array and an optionally specified IComparer interface.

Example

     Public Sub SortArray(  )        ' ----- Start with the unsorted data.        Dim counter As Integer        Dim dataSet(  ) As Integer = {9, 8, 12, 4, 5}        For counter = 0 To 4           Console.Write(CStr(dataSet(counter)) & " ")        Next counter        Console.WriteLine("")        ' ----- Sort and display the data.        System.Array.Sort(dataSet)        Console.WriteLine("Sorted:")        For counter = 0 To 4           Console.Write(CStr(dataSet(counter)) & " ")        Next counter        Console.WriteLine("")     End Sub 

The output is:

     9 8 12 4 5     Sorted:     4 5 8 9 12 

See Also

Array Class, Array.Reverse Method




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net