SortArray |
Arrays | |
Aptilis 1 |
SortArray(ArrayName[], stringHow)
SortArray will sort an array. You choose to sort alphabetically or
numerically.
You have to keep in mind that all the empty values will be put at
the begining of the array.
For key based arrays, the values are also reordered, and that affects
how values are retrieved by getNext and
getPrevious,
and the same keys are still attached to the same values.
The second paremeter is either "numeric" or "alphabetical"
to indicate how you want the variables to be sorted.
Use
SortDatabase
to sort databases.
Return value:
0 if everything is OK.
-1 if there has been a problem and the variable _errno contains the cause of the error.
Example 1:
Sorting an array numerically. Note that strings containing only letters have a value of 0.
m[0] = 52.253 m[1] = "1" $ m[2] = "12.5" $ m[3] = 34 m[4] = "Hello" $ m[5] = 1000 m[6] = -27 m[7] = -35 m[8] = "5" $ sortarray(m[], "numeric") n = getarraysize(m[]) for i=0 to n - 1 print("SORTED: ", m[i]$, "\n") end forResult:
SORTED: -35.000000 SORTED: -27.000000 SORTED: Hello SORTED: 1 SORTED: 5 SORTED: 12.5 SORTED: 34.000000 SORTED: 52.253000 SORTED: 1000.000000 |
Example 2:
Sorting an array alphabetically.
m[0] = "Bonjour" $ m[1] = "Hello" $ m[2] = "Ciao" $ m[3] = "Arivederci" $ m[4] = "Salut" $ m[5] = "Morning!" $ m[6] = "Hasta luego" $ m[7] = "Guten Tag" $ m[8] = "Sayonara" $ sortarray(m[], "alphabetical") n = getarraysize(m[]) for i=0 to n - 1 print("SORTED: ", m[i]$, "\n") end forResult:
SORTED: Arivederci SORTED: Bonjour SORTED: Ciao SORTED: Guten Tag SORTED: Hasta luego SORTED: Hello SORTED: Morning! SORTED: Salut SORTED: Sayonara |
Example 3:
Sorting a key based array alphabetically.
m["Hello"] = "Bonjour" $ m["Bye"] = "Au revoir" $ m["Speak to you soon"] = "A bientôt" $ m["Ta"] = "Ciao" $ sortarray(m[], "alphabetical") setArrayIndex(m[], "begin") n = getarraysize(m[]) for i=0 to n - 1 print("SORTED: ", getNext(m[])$, "\n") end forResult:
SORTED: A bientôt SORTED: Au revoir SORTED: Bonjour SORTED: Ciao |
See also
SetArrayIndex,
GetPrevious,
GetArraySize.