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, got "not_a_function" instead 79 In line 2, byte 34: 80 81 ` sort([3, 1, 2], "not_a_function")` 82 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ 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 ~~~~^ 118 119 120 -- End -- 121 122 123 Growing an array from its comparator must not invalidate the storage 124 being sorted. The array becomes mutable again after the exception. 125 126 -- Testcase -- 127 {% 128 let expected = []; 129 130 for (let i = 0; i < 64; i++) 131 push(expected, i); 132 133 let input = reverse(expected); 134 let calls = 0; 135 136 try { 137 sort(input, (a, b) => { 138 calls++; 139 140 for (let i = 0; i < 1000; i++) 141 push(input, i); 142 143 return a - b; 144 }); 145 die("Mutation was not rejected"); 146 } 147 catch (e) { 148 assert(e.type == "Type error" && e.message == "array value is immutable"); 149 } 150 151 assert(calls == 1 && length(input) == 64); 152 assert(join(",", sort(input)) == join(",", expected)); 153 push(input, 64); 154 assert(pop(input) == 64 && length(input) == 64); 155 print("Array growth rejected; mutability restored\n"); 156 %} 157 -- End -- 158 159 -- Expect stdout -- 160 Array growth rejected; mutability restored 161 -- End -- 162 163 164 Other array mutations, including recursive sorting, are rejected too. 165 166 -- Testcase -- 167 {% 168 for (let mutate in [ 169 (a) => pop(a), 170 (a) => shift(a), 171 (a) => unshift(a, 0), 172 (a) => splice(a, 0, 1), 173 (a) => splice(a, 0, 0, 4, 5), 174 (a) => { a[1000] = 4; }, 175 (a) => { a[0] = 4; }, 176 (a) => { delete a[0]; }, 177 (a) => sort(a) 178 ]) { 179 let input = [ 3, 2, 1 ]; 180 let calls = 0; 181 182 try { 183 sort(input, (a, b) => { 184 calls++; 185 mutate(input); 186 return a - b; 187 }); 188 die("Mutation was not rejected"); 189 } 190 catch (e) { 191 assert(e.type == "Type error" && e.message == "array value is immutable"); 192 } 193 194 assert(calls == 1 && join(",", sort(input)) == "1,2,3"); 195 push(input, 4); 196 assert(pop(input) == 4); 197 } 198 199 print("Array mutations and recursive sorting rejected\n"); 200 %} 201 -- End -- 202 203 -- Expect stdout -- 204 Array mutations and recursive sorting rejected 205 -- End -- 206 207 208 Growing or deleting object entries from a comparator must not invalidate 209 the hash table entry pointers saved by the sort operation. 210 211 -- Testcase -- 212 {% 213 for (let mutate in [ 214 (o) => { for (let i = 0; i < 100; i++) o[`key${i}`] = i; }, 215 (o) => { delete o.a; }, 216 (o) => { o.a = 4; }, 217 (o) => sort(o) 218 ]) { 219 let input = { c: 3, b: 2, a: 1 }; 220 let calls = 0; 221 222 try { 223 sort(input, (k1, k2) => { 224 calls++; 225 mutate(input); 226 return k1 < k2 ? -1 : k1 > k2 ? 1 : 0; 227 }); 228 die("Mutation was not rejected"); 229 } 230 catch (e) { 231 assert(e.type == "Type error" && e.message == "object value is immutable"); 232 } 233 234 assert(calls == 1 && length(input) == 3); 235 assert(input.a == 1 && input.b == 2 && input.c == 3); 236 assert(join(",", keys(sort(input))) == "a,b,c"); 237 input.d = 4; 238 assert(delete input.d); 239 assert(length(input) == 3); 240 } 241 242 print("Object mutations and recursive sorting rejected\n"); 243 %} 244 -- End -- 245 246 -- Expect stdout -- 247 Object mutations and recursive sorting rejected 248 -- End -- 249 250 251 A comparator may handle a rejected mutation and continue sorting. It may 252 also sort and modify other collections, and modify nested values. 253 254 -- Testcase -- 255 {% 256 let input = [ { n: 3 }, { n: 2 }, { n: 1 } ]; 257 let other = [ 2, 1 ]; 258 let object = { b: 2, a: 1 }; 259 let calls = 0; 260 261 assert(sort(input, (a, b) => { 262 calls++; 263 a.seen = true; 264 b.seen = true; 265 push(other, 0); 266 sort(other); 267 object.c = 3; 268 sort(object); 269 270 try { 271 push(input, null); 272 die("Mutation was not rejected"); 273 } 274 catch (e) { 275 assert(e.type == "Type error" && e.message == "array value is immutable"); 276 } 277 278 return a.n - b.n; 279 }) == input); 280 281 assert(calls > 0 && join(",", map(input, (v) => v.n)) == "1,2,3"); 282 assert(input[0].seen && input[1].seen && input[2].seen); 283 push(input, { n: 4 }); 284 assert(pop(input).n == 4); 285 assert(join(",", keys(object)) == "a,b,c"); 286 print("Handled mutation errors and independent sorts work\n"); 287 %} 288 -- End -- 289 290 -- Expect stdout -- 291 Handled mutation errors and independent sorts work 292 -- End -- 293 294 295 The default comparator must reject mutations from tostring() methods and 296 propagate their exceptions before invoking any further conversions. 297 298 -- Testcase -- 299 {% 300 let input; 301 let calls = 0; 302 let item = proto({}, { tostring: function() { 303 calls++; 304 push(input, 4); 305 return "item"; 306 } }); 307 input = [ item, item, item ]; 308 309 try { 310 sort(input); 311 die("Mutation was not rejected"); 312 } 313 catch (e) { 314 assert(e.type == "Type error" && e.message == "array value is immutable"); 315 } 316 317 assert(calls == 1 && length(input) == 3); 318 push(input, 4); 319 assert(pop(input) == 4); 320 print("String conversion mutation rejected; mutability restored\n"); 321 %} 322 -- End -- 323 324 -- Expect stdout -- 325 String conversion mutation rejected; mutability restored 326 -- End -- 327 328 329 An exception in either operand's conversion stops all later conversions. 330 331 -- Testcase -- 332 {% 333 for (let fail_at in [ 1, 2 ]) { 334 let calls = 0; 335 let item = proto({}, { tostring: function() { 336 if (++calls == fail_at) 337 die("Sort conversion failed"); 338 339 return "item"; 340 } }); 341 let input = [ item, item, item ]; 342 343 try { 344 sort(input); 345 die("Conversion exception was not propagated"); 346 } 347 catch (e) { 348 assert(e.message == "Sort conversion failed"); 349 } 350 351 assert(calls == fail_at && length(input) == 3); 352 push(input, 4); 353 assert(pop(input) == 4); 354 } 355 356 print("String conversion exceptions stop subsequent callbacks\n"); 357 %} 358 -- End -- 359 360 -- Expect stdout -- 361 String conversion exceptions stop subsequent callbacks 362 -- End -- 363 364 365 Mutability is restored after successful sorting as well as comparator 366 exceptions, without changing the identity or prototype of the input. 367 368 -- Testcase -- 369 {% 370 let prototype = { marker: true }; 371 let input = proto([ 3, 2, 1 ], prototype); 372 let object = proto({ c: 3, b: 2, a: 1 }, prototype); 373 374 assert(sort(input) == input && proto(input) == prototype); 375 assert(sort(object) == object && proto(object) == prototype); 376 push(input, 4); 377 object.d = 4; 378 assert(join(",", input) == "1,2,3,4"); 379 assert(join(",", keys(object)) == "a,b,c,d"); 380 381 for (let source in [ input, object ]) { 382 try { 383 sort(source, () => die("Sort comparator failed")); 384 die("Comparator exception was not propagated"); 385 } 386 catch (e) { 387 assert(e.message == "Sort comparator failed"); 388 } 389 } 390 391 assert(pop(input) == 4); 392 assert(delete object.d); 393 assert(proto(input) == prototype && proto(object) == prototype); 394 print("Mutability, identity and prototypes preserved\n"); 395 %} 396 -- End -- 397 398 -- Expect stdout -- 399 Mutability, identity and prototypes preserved 400 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt