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