Sort

From NSB App Studio
Revision as of 15:34, 25 March 2019 by Ghenne (talk | contribs) (→‎Output)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This function is for BASIC compatibility. It is not available in pure JavaScript projects.

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 (BASIC)

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