SortDatabase

Databases
Aptilis 2

SortDatabase(db[],WhatField,sortType)

SortDatabase(db[], whatField, sortType) will sort a database using one of its fields as a sorting criteria.
Indicate the field to sort by in WhatField, 0 being the first one, 1, the second one, etc.
Sorting can be done alphabetically or numerically by setting 'sortType' to either "alphabetical" or "numeric".
To sort in reverse order, sort normally, and then use reverseArray.

Use sortArrays on normal arrays arrays (not databases).

Return value:
0 if everything was OK or -1 if there wasn't enough memory to carry out the operation.

Example:

sub splash(title)



print(title$, "\n", string(len(title$), "-")$, "\n")

n = getArraySize(_animals[])
for i=0 to n - 1

getAllFields(fls[], _animals[i]$)
print(join(fls[], " * ")$, "\n")

end for


print("\n")

end splash


sub main()


loadDatabase(_animals[], "animals.txt")


splash("Rough")


sortDatabase(_animals[], 2, "numeric")
splash("Numeric")


sortDatabase(_animals[], 0, "alphabetical")
splash("Alpha->by English names")

sortDatabase(_animals[], 1, "alphabetical")
splash("Alpha->by French names")




end main

Result:
Rough
-----
Dog * Chien * 14 * mammal * shoes
Cat * Chat * 3 * flying mammal * flies
Freddy the Goldfish * Fred Le Poisson rouge * 0.5 * fish * canned fish food
Teebo * Thibault * 29 * apparantly mammal * pizza
Bird * Oiseau * 2 * Dinosaur * insects

Numeric
-------
Freddy the Goldfish * Fred Le Poisson rouge * 0.5 * fish * canned fish food
Bird * Oiseau * 2 * Dinosaur * insects
Cat * Chat * 3 * flying mammal * flies
Dog * Chien * 14 * mammal * shoes
Teebo * Thibault * 29 * apparantly mammal * pizza

Alpha->by English names
-----------------------
Bird * Oiseau * 2 * Dinosaur * insects
Cat * Chat * 3 * flying mammal * flies
Dog * Chien * 14 * mammal * shoes
Freddy the Goldfish * Fred Le Poisson rouge * 0.5 * fish * canned fish food
Teebo * Thibault * 29 * apparantly mammal * pizza

Alpha->by French names
----------------------
Cat * Chat * 3 * flying mammal * flies
Dog * Chien * 14 * mammal * shoes
Freddy the Goldfish * Fred Le Poisson rouge * 0.5 * fish * canned fish food
Bird * Oiseau * 2 * Dinosaur * insects
Teebo * Thibault * 29 * apparantly mammal * pizza
There are other database functions that allow you to isolate one or all the fields from a record, (getField or getAllFields) and to search a database that has been loaded for a given record, according to one field (getRecordIndexByKey, getRecordIndexByNearKey).