-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiColumnSort.js
More file actions
35 lines (31 loc) · 1.04 KB
/
multiColumnSort.js
File metadata and controls
35 lines (31 loc) · 1.04 KB
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
function sortMethodAsc(a, b) {
return a === b ? 0 : a > b ? 1 : -1;
}
function sortMethodDesc(a, b) {
return -sortMethodAsc(a, b);
}
function sortMethodWithDirection(direction = "asc") {
return direction === "asc" ? sortMethodAsc : sortMethodDesc
}
function sortMethodWithDirectionByColumn(columnName, direction){
const sortMethod = sortMethodWithDirection(direction)
return function(a, b){
return sortMethod(a[columnName], b[columnName]);
}
}
function sortMethodWithDirectionMultiColumn(sortArray = []) {
//sample of sortArray
// sortArray = [
// { column: "column5", direction: "asc" },
// { column: "column3", direction: "desc" }
// ]
const sortMethodsForColumn = sortArray.map( item => sortMethodWithDirectionByColumn(item.column, item.direction) );
return function(a,b) {
let sorted = 0;
let index = 0;
while (sorted === 0 && index < sortMethodsForColumn.length) {
sorted = sortMethodsForColumn[index++](a,b);
}
return sorted;
}
}