summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/fast/js/script-tests/comparefn-sort-stability.js
blob: 5cdab104f844c430697dda3bf28dec544ccd900d (plain)
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
description(
"This tests that sort(compareFn) is a stable sort."
);

function clone(source, target) {
    for (i = 0; i < source.length; i++) {
        target[i] = source[i];
    }
}

var arr = [];
arr[0] = new Number(1);
arr[1] = new Number(2);
arr[2] = new Number(1);
arr[3] = new Number(2);

var sortArr = [];
clone(arr, sortArr);
sortArr.sort(function(a,b) { return a - b; });

shouldBe('arr[0]', 'sortArr[0]');
shouldBe('arr[1]', 'sortArr[2]');
shouldBe('arr[2]', 'sortArr[1]');
shouldBe('arr[3]', 'sortArr[3]');

// Just try again...
sortArr.sort(function(a,b) { return a - b; });
shouldBe('arr[0]', 'sortArr[0]');
shouldBe('arr[1]', 'sortArr[2]');
shouldBe('arr[2]', 'sortArr[1]');
shouldBe('arr[3]', 'sortArr[3]');