SORT
SORT byref A [USE cmpfunc( x, y )]
Sorts an array A
in ascending order. The sorted array is
return as A
, therefore overwriting the initial array. If a
compare function cmpfunc
is specified, this function will
be used for comparision. The compare function takes two elements of
A
as x
, y
to compare and must
return:
-1
ifx
is to be placed beforey
1
ify
is to be placed beforex
0
if it doesn’t matter which is placed first (which is usually the case when the elements are equal)
Example 1: Sorting in ascending order
5, 3, 8, 2, 1, 7, 9]
A = [sort A
print A ' Output [1,2,3,5,7,8,9]
Example 2: Sorting in ascending order using a compare function
func cmpfunc_ascending(x, y)
if x == y
return 0
elseif x > y
return 1
else
return -1
endif
end
5, 3, 8, 2, 1, 7, 9]
A = [sort A use cmpfunc_ascending(x, y)
print A ' Output [1,2,3,5,7,8,9]
Example 3: Sorting in descending order using a compare function
func cmpfunc_descending(x, y)
if x == y
return 0
elseif x < y
return 1
else
return -1
endif
end
5, 3, 8, 2, 1, 7, 9]
A = [sort A use cmpfunc_descending(x, y)
print A ' Output [9,8,7,5,3,2,1]
Code samples using SORT
Data
APPEND
ARRAY
DATA
DELETE
DIM
EMPTY
ERASE
INSERT
ISARRAY
ISDIR
ISFILE
ISLINK
ISMAP
ISNUMBER
ISSTRING
LBOUND
LEN
READ
REDIM
RESTORE
SEARCH
SORT
SWAP
UBOUND
If there is insufficient information on this page and you wish learn more about SORT,
please send an email to smallbasic@gmail.com. You can help to improve information about
SORT by submitting a pull request,
click View Source for details. Note, an offline language reference text file is also available - see the Download section.