1Array.sort(3kaya) Kaya module reference Array.sort(3kaya)
2
3
4
6 Array::sort - Sort an array.
7
9 Void sort( var [a] xs, Int(a, a) sortfn=compare )
10
12 xs The array to sort
13
14 sortfn Optionally, a user-defined comparison function.
15
17 Sort an array in-place using the quicksort algorithm. By default this
18 uses the Builtins.compare (3kaya) function and sorts in ascending
19 order, but it can instead use a user supplied compare function.
20
21
22 xs = [3,5,1,2,6,1];
23 sort(xs); // [1,1,2,3,5,6]
24
25 Comparison functions should return 0 if the values are identical, less
26 than zero if the first value passed to the function is 'smaller', and
27 more than zero if the second value is 'bigger'. In this context,
28 'smaller' values are moved to the start of the array, and 'bigger' val‐
29 ues to the end. The following example has a simple function to sort an
30 array of Int s into descending order.
31
32
33 Int rsort(Int a, Int b) {
34 return b-a;
35 }
36
37 Void main() {
38 xs = [3,5,1,2,6,1];
39 sort(xs,rsort); // [6,5,3,2,1,1]
40 }
41
42 Values that are identical for the purposes of the sorting function will
43 be placed in an undefined order relative to each other.
44
46 Kaya standard library by Edwin Brady, Chris Morris and others
47 (kaya@kayalang.org). For further information see http://kayalang.org/
48
50 The Kaya standard library is free software; you can redistribute it
51 and/or modify it under the terms of the GNU Lesser General Public
52 License (version 2.1 or any later version) as published by the Free
53 Software Foundation.
54
56 The Array.sorted (3kaya) function returns a sorted copy of the array,
57 rather than sorting in place.
58
59
60
61Kaya December 2010 Array.sort(3kaya)