Sort: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(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 ...")
 
No edit summary
Line 1: Line 1:
SORT(''array''[,''sortfunction''])
Sort(''array''[,''sortfunction''])


'''Description'''
== 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.
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 ==


<pre>
<pre>
Line 25: Line 25:
</pre>
</pre>


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


<pre>
<pre>
Line 33: Line 33:
sorted alphabetically:10,100,30,4,700
sorted alphabetically:10,100,30,4,700
</pre>
</pre>
[[Category:Language Reference]]

Revision as of 03:19, 17 August 2012

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