-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort.js
More file actions
32 lines (24 loc) · 769 Bytes
/
sort.js
File metadata and controls
32 lines (24 loc) · 769 Bytes
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
// 需要被排序的数组
var data1 = new Date().getTime()
var list = ['Delta', 'alpha', 'CHARLIE', 'bravo'];
// 对需要排序的数字和位置的临时存储
var mapped = list.map(function(el, i) {
return { index: i, value: el.toLowerCase() };
})
// 按照多个值排序数组
mapped.sort(function(a, b) {
return +(a.value > b.value) || +(a.value === b.value) - 1;
});
// 根据索引得到排序的结果
var result = mapped.map(function(el){
return list[el.index];
});
console.log(result)
console.log(new Date().getTime()-data1)
var data1 = new Date().getTime()
var list = ['Delta', 'alpha', 'CHARLIE', 'bravo'];
list.sort(function(a, b) {
return +(a > b) || +(a === b) - 1;
});
console.log(list)
console.log(new Date().getTime()-data1)