forked from alibaba/nquery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorderby.js
55 lines (48 loc) · 1.23 KB
/
orderby.js
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
// (C) 2011-2013 Alibaba Group Holding Limited.
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// Author :windyrobin <[email protected]>
var AstHelper = require('../lib/ast_helper');
module.exports = orderby;
function debug(str){
//console.log(str);
}
function inspect(obj){
//console.log(require("util").inspect(obj, false, 10));
}
/**
* @param {Array} orderby [{name : 'col1' , type : "ASC"}, ...]
*/
function orderby(dc, orderby) {
var cPos = [];
var dCols = dc.columns;
var dData = dc.data;
var pos;
for (var i = 0; i < orderby.length ; i++) {
pos = AstHelper.getRowPosByCol(dCols, orderby[i].name);
cPos.push(pos);
}
//debug('orderby pos');
//inspect(cPos);
var rData = dData.sort(function(a, b){
var cmp = 0;
for(var i = 0; i < cPos.length; i++){
var pos = cPos[i];
if (a[pos] != b[pos]) {
if (a[pos] > b[pos]) {
cmp = 1;
} else {
cmp = -1;
}
if(orderby[i].type == 'DESC') cmp = -cmp;
break;
}
}
return cmp;
});
return {
columns : dCols,
data : rData
}
}