一个js排序
1 var rows=[
2 [4,"000004",1],
3 [5,"600000",2],
4 [1,"000001",1],
5 [2,"000002",1],
6 [3,"000003",1],
7 [6,"600002",2],
8 [7,"600003",2],
9 [8,"300001",2],
10 ]
11 //a:索引,b:排序方向,c:类型,0:数字,1:中文
12 var compare = function(a, b, c) {
13 //比较的两个值
14 return function(d, e) {
15 var g;
16 d = d[a];
17 e = e[a];
18 var f = b == 1 ? false: true;
19 if (c == 0) {
20 d = typeof d == "undefined" || d.toString() == "" ? (-999999*b): eval(d);
21 e = typeof e == "undefined" || e.toString() == "" ? (-999999*b): eval(e);
22 g = f ? d > e ? 1: d < e ? -1: 0: d < e ? 1: d > e ? -1: 0
23 } else if (c == 1) {
24 d = typeof d == "undefined" ? "": d;
25 e = typeof e == "undefined" ? "": e;
26 return f ? d.localeCompare(e) : e.localeCompare(d)
27 }
28 return g
29 }
30 };
31 rows.sort(compare(2,1,0));
32 for(var i=0;i<rows.length;i++){
33 //alert(rows[i]);
34 }
2 [4,"000004",1],
3 [5,"600000",2],
4 [1,"000001",1],
5 [2,"000002",1],
6 [3,"000003",1],
7 [6,"600002",2],
8 [7,"600003",2],
9 [8,"300001",2],
10 ]
11 //a:索引,b:排序方向,c:类型,0:数字,1:中文
12 var compare = function(a, b, c) {
13 //比较的两个值
14 return function(d, e) {
15 var g;
16 d = d[a];
17 e = e[a];
18 var f = b == 1 ? false: true;
19 if (c == 0) {
20 d = typeof d == "undefined" || d.toString() == "" ? (-999999*b): eval(d);
21 e = typeof e == "undefined" || e.toString() == "" ? (-999999*b): eval(e);
22 g = f ? d > e ? 1: d < e ? -1: 0: d < e ? 1: d > e ? -1: 0
23 } else if (c == 1) {
24 d = typeof d == "undefined" ? "": d;
25 e = typeof e == "undefined" ? "": e;
26 return f ? d.localeCompare(e) : e.localeCompare(d)
27 }
28 return g
29 }
30 };
31 rows.sort(compare(2,1,0));
32 for(var i=0;i<rows.length;i++){
33 //alert(rows[i]);
34 }