-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilOrdenamiento.js
48 lines (46 loc) · 1.48 KB
/
utilOrdenamiento.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
function sortJSONSingle(data, key, orden) {
return data.sort( function(a, b) {
let x = a[key],
y = b[key];
if (orden === 'asc') {
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
if (orden === 'desc') {
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
}
});
}
function sortJSONMulti(data, sortBy, orden) {
let direction;
data.sort( function(a, b) {
let i = 0, result = 0;
while(i < sortBy.length && result === 0) {
direction = (orden[i] === "asc")? -1: 1;
result = direction*(a[ sortBy[i] ].toString() < b[ sortBy[i] ].toString() ? -1 : (a[ sortBy[i] ].toString() > b[ sortBy[i] ].toString() ? 1 : 0));
i++;
}
return result;
})
}
function sortJSONMulti2(data, sortBy, orden) {
let direction;
data.sort( function(a, b) {
let i = 0, result = 0;
while(i < sortBy.length && result === 0) {
direction = (orden[i] === "asc")? -1: 1;
result = sortBy[i].direction*(a[ sortBy[i].prop ].toString() < b[ sortBy[i].prop ].toString() ? -1 : (a[ sortBy[i].prop ].toString() > b[ sortBy[i].prop ].toString() ? 1 : 0));
i++;
}
return result;
})
}
// Ordenamos el json.
// Ojo, hay que incluir como parámetro:
// El JSON origninal
// La propiedad que queremos ordenar
// El orden (asc | desc)
//var oJSON = sortJSON(elJSON, 'num', 'asc');
module.exports = {
sortJSONSingle,
sortJSONMulti
}