Sort: Difference between revisions
Jump to navigation
Jump to search
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
''This function is for BASIC compatibility. It is not available in pure JavaScript projects.'' | |||
Sort(''array''[,''sortfunction'']) | Sort(''array''[,''sortfunction'']) | ||
Line 7: | Line 9: | ||
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. | 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 == | == Example (BASIC) == | ||
<pre> | <pre> | ||
Line 37: | Line 39: | ||
[[Category:Variable Handling]] | [[Category:Variable Handling]] | ||
[[Category:BASIC Functions]] |
Latest revision as of 15:34, 25 March 2019
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