SortStructuredArray()

Syntax

SortStructuredArray(ArrayName(), Options, OffsetOf(Structure\Field), TypeOf(Structure\Field) [, Start, End])
Description
Sorts the specified structured array, according to the given options. The array must have an associated structure.

Parameters

ArrayName() The array to sort.
Options It can be a combination of the following values:
  #PB_Sort_Ascending : Sort the array in ascending order (lower values first)
  #PB_Sort_Descending: Sort the array in descending order (higher values first)
  #PB_Sort_NoCase    : Sort the string array without case sensitive (a=A, b=B etc..)
OffsetOf(Structure\Field) Offset of the field in the structure. OffsetOf() may be used to retrieve the field offset in the structure associated to the array.
TypeOf(Structure\Field) The field type of the field in the structure. It has to match the real structure field type. TypeOf() may be used to automatically retrieve the field type. Available types are:
  #PB_Byte     : The structure field to sort is a byte (.b)
  #PB_Word     : The structure field to sort is a word (.w)
  #PB_Long     : The structure field to sort is a long (.l)
  #PB_String   : The structure field to sort is a string (.s or $, fixed strings are not supported)
  #PB_Float    : The structure field to sort is a float (.f)
  #PB_Double   : The structure field to sort is a double (.d)
  #PB_Quad     : The structure field to sort is a quad (.q)
  #PB_Character: The structure field to sort is a character (.c)
  #PB_Integer  : The structure field to sort is an integer (.i)
  #PB_Ascii    : The structure field to sort is an ascii character (.a)
  #PB_Unicode  : The structure field to sort is a unicode character (.u)
Start, End (optional) The index of the first and last element in the array that should be sorted. If these parameters are not specified, then the whole array is sorted.

Remarks

Fixed strings are not supported by the sort routine. If an array is not fully filled then null elements will be sorted first in ascending order and last in descending order.
NaN numbers are not accepted when sorting as it produces random results.

Example


  Structure Animal
    Name$
    Speed.l
  EndStructure

  Dim Animals.Animal(2)

  Animals(0)\Name$ = "Tiger"
  Animals(0)\Speed = 10

  Animals(1)\Name$ = "Jaguar"
  Animals(1)\Speed = 40

  Animals(2)\Name$ = "Zebra"
  Animals(2)\Speed = 30

  SortStructuredArray(Animals(), 0, OffsetOf(Animal\Name$), TypeOf(Animal\Name$))

  For k=0 To 2
    Debug Animals(k)\Name$+" - Speed: "+Str(Animals(k)\Speed)
  Next

  SortStructuredArray(Animals(), 0, OffsetOf(Animal\Speed), TypeOf(Animal\Speed))

  For k=0 To 2
    Debug Animals(k)\Name$+" - Speed: "+Str(Animals(k)\Speed)
  Next

See Also

SortArray(), RandomizeArray()

Supported OS

All

<- SortList() - Sort Index - SortStructuredList() ->