forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_sort.js
More file actions
91 lines (65 loc) · 1.83 KB
/
array_sort.js
File metadata and controls
91 lines (65 loc) · 1.83 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
function write(args)
{
WScript.Echo(args);
}
//Array sort testing for Array type
var x = [120, 5, 8, 4, 6, 9, 9, 10, 2, 3];
function c(a,b) {return a - b}
write(x.sort());
write(x.sort(c));
function sort_func(x, y) {
return (x > y) ? 1 : ((x < y) ? -1 : 0);
}
var sorted = x.sort(sort_func);
if (sorted[0] != 2 || sorted[9] != 120) write("Array `sort` failed!");
//Sort numerically and ascending:
var myarray=[25, 8, 7, 41]
write(myarray.sort(function(a,b){return a - b}))
//Sort numerically and descending:
var myarray2=[25, 8, 7, 41]
write(myarray.sort(function(a,b){return b - a})) //Array now becomes [41, 25, 8, 71
var mystr = new Array("some", "sample", "strings", "for", "testing");
write(mystr.sort());
write(mystr.sort(function(a,b){return a - b}) + " - Output different in cscript due to NaN");
var a;
function setup(size) {
var i;
a=new Array();
for (i=0;i<size;i++)
a[a.length]=(size/2)-i;
}
function numeric(a,b) {return a - b}
function test() {
a.sort(numeric);
}
setup(10);
test();
setup(100);
test();
setup(1000);
test();
function compare(a,b)
{
this.xyz = 10;
return a - b;
}
var testThis = [1,2,3];
write(testThis.sort(compare));
write(xyz);
a = [ 1, 1.2, 12, 4.8, 4 ];
write(a.sort(function(x, y) { return x - y }));
a = [3, 2, 1];
try
{
// throws
a.sort(null);
} catch (e) {
write(e);
}
// default comparer
a.sort(undefined);
write(a);