1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
The `sort()` function performs an in-place sorting on the given array,
invoking the specified callback (if any) to compare items during the
sort process.
If no callback is given or if the callback argument is `null`, a default
comparator function is used which will sort number values numerically
and all other value types lexically.
Returns the sorted input array.
Returns `null` if the given input array value is not an array.
-- Testcase --
{%
print(join("\n", [
// default numeric sort
sort([ 6, 4.3, 1, 45, 3.01, 2 ]),
// default lexical sort
sort([ "qrx", "bar", "foo", "abc" ]),
// default lexical sort due to implicit stringification
sort([ true, false, null, 1, "2b" ]),
// sort with custom callback (by word length)
sort([ "apple", "pear", "banana", "grapefruit" ], (a, b) => length(a) - length(b)),
// sort with custom callback (by type, then value)
sort([ 4, 1, 9, 2, "x", "a", "q", "b" ], (a, b) => {
let t1 = type(a), t2 = type(b);
if (t1 < t2)
return -1;
else if (t1 > t2)
return 1;
if (a < b)
return -1;
else if (a > b)
return 1;
return 0;
})
]), "\n");
%}
-- End --
-- Expect stdout --
[ 1, 2, 3.01, 4.3, 6, 45 ]
[ "abc", "bar", "foo", "qrx" ]
[ 1, "2b", false, null, true ]
[ "pear", "apple", "banana", "grapefruit" ]
[ 1, 2, 4, 9, "a", "b", "q", "x" ]
-- End --
Supplying an invalid callback will trigger an exception.
-- Testcase --
{%
sort([3, 1, 2], "not_a_function")
%}
-- End --
-- Expect stderr --
Type error: left-hand side is not a function
In line 2, byte 34:
` sort([3, 1, 2], "not_a_function")`
Near here --------------------------^
-- End --
Supplying an invalid array will yield `null`.
-- Testcase --
{%
printf("%.J\n", sort("not_an_array", function(a, b) { return a - b }));
%}
-- End --
-- Expect stdout --
null
-- End --
Exceptions in the callback terminate the sort process and are
propagated to the calling context.
-- Testcase --
{%
sort([ 1, 2, 3 ], function() { die() });
%}
-- End --
-- Expect stderr --
Died
In [anonymous function](), line 2, byte 37:
called from function sort ([C])
called from anonymous function ([stdin]:2:40)
` sort([ 1, 2, 3 ], function() { die() });`
Near here -----------------------------^
-- End --
|