-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiColumnSort2.js
More file actions
30 lines (26 loc) · 971 Bytes
/
multiColumnSort2.js
File metadata and controls
30 lines (26 loc) · 971 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
const sortMethodAsc = (a, b) => (a === b ? 0 : a > b ? 1 : -1);
const sortMethodDesc = (a, b) => -sortMethodAsc(a, b);
const sortMethodWithDirection = (direction = 'asc') =>
direction === 'asc' ? sortMethodAsc : sortMethodDesc;
const sortMethodWithDirectionByColumn = (columnName, direction) => {
const sortMethod = sortMethodWithDirection(direction);
return (a, b) => sortMethod(a[columnName], b[columnName]);
};
const 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 (a, b) => {
let sorted = 0,
index = 0;
while (sorted === 0 && index < sortMethodsForColumn.length) {
sorted = sortMethodsForColumn[index++](a, b);
}
return sorted;
};
};