Sort

From NSB App Studio
Revision as of 23:44, 8 July 2012 by Brendon (talk | contribs) (Created page with "SORT(''array''[,''sortfunction'']) '''Description''' SORT returns ''array'' as a sorted array in ascending order. If the elements are all strings or all numbers, it will be ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

SORT(array[,sortfunction])

Description

SORT returns array as a sorted array in ascending order. If the elements are all strings or all numbers, it will be properly sorted.

The optional sortfunction argument can be used to do a custom sort. If you supply just the letter “d” as the second argument, it will sort in descending order. You can also specify your own compare function. It takes two arguments. Return a negative number if the first argument is less than the second. Return a positive number if the first argument is greater than the second.

Example

myArray=[100,10,700,4,30]
Print "orginal array:" & myArray
Print "sorted:" & Sort(myArray)
Print "sorted descending:" & Sort(myArray,"d")
Print "sorted alphabetically:" & Sort(myArray,mySort)
 
Function mySort(x,y)
  If (CStr(x)<CStr(y)) Then
    mySort=-1
  Else
    mySort=1
  End If
End Function

Output

orginal array:100,10,700,4,30
sorted:4,10,30,100,700
sorted descending:700,100,30,10,4
sorted alphabetically:10,100,30,4,700