Sort: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
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 ..."
 
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
SORT(''array''[,''sortfunction''])
''This function is for BASIC compatibility. It is not available in pure JavaScript projects.''  


'''Description'''
Sort(''array''[,''sortfunction''])


SORT returns ''array'' as a sorted array in ascending order. If the elements are all strings or all numbers, it will be properly sorted.
== 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.
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 25: Line 27:
</pre>
</pre>


'''Output'''
== Output ==


<pre>
<pre>
Line 33: Line 35:
sorted alphabetically:10,100,30,4,700
sorted alphabetically:10,100,30,4,700
</pre>
</pre>
[[Category:Language Reference]]
[[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