Clib Sort Array Method

The Clib Sort Array method sorts elements in an array, starting with index 0, and then continuing to the value that you specify in the elementCount argument minus 1. This method differs from the Sort Array method in standard JavaScript in the following ways:

  • The Clib Sort Array method can sort a dynamically created array.

  • The Sort Array method in standard JavaScript works only with an array that an Array statement explicitly creates.

Format

Clib.qsort(array, [elementCount, ]compareFunction)

The following table describes the arguments for the Clib Sort Array method.

Argument Description

array

The array that this method sorts.

elementCount

The number of elements in the array, up to 65,536.

If you do not specify the elementCount argument, then this method sorts the entire array.

compareFunction

A custom function that can affect the sort order.

Example

The following example prints a list of colors sorted in reverse alphabetical order, ignoring case:

// initialize an array of colors
   var colors = { "yellow", "Blue", "GREEN", "purple", "RED", 
"BLACK", "white", "orange" }; 
// sort the list using qsort and our ColorSorter routine
   Clib.qsort(colors,ReverseColorSorter); 
// display the sorted colors
   for ( var i = 0; i <= getArrayLength(colors); i++ )
   Clib.puts(colors[i]);

   function ReverseColorSorter(color1, color2)
// do a simple string that is not case-sensitive
// comparison, and reverse the results too 
{
   var CompareResult = Clib.stricmp(color1,color2)
   return( _CompareResult ); 
}

This example produces the following output:

yellow 
white 
RED
purple
orange
GREEN 
Blue
BLACK

Related Topics

For more information, see Sort Array Method.