Skip to content

Commit 7872a53

Browse files
committed
feat: sort by multiple keys
1 parent b3c9429 commit 7872a53

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
}
303303

304304
if (Selector) {
305+
// let selectors = Selector.split(/,(?![^()]*\))/g);
305306

306307
let selectors = Selector.split(',');
307308
for (let j = 0; j < selectors.length; j++) {
@@ -654,7 +655,38 @@
654655
return true
655656
}
656657

658+
657659
function sortData(data, sort) {
660+
return data.sort((a, b) => {
661+
for (let i = 0; i < sort.length; i++) {
662+
let key = sort[i].key;
663+
if (a[key] == null && b[key] == null) continue;
664+
if (a[key] == null)
665+
return sort[i].direction === 'desc' ? -1 : 1;
666+
if (b[key] == null)
667+
return sort[i].direction === 'desc' ? 1 : -1;
668+
669+
if (typeof a[key] !== typeof b[key]) {
670+
return typeof a[key] < typeof b[key] ? -1 : 1;
671+
}
672+
673+
if (a[key] !== b[key]) {
674+
if (typeof a[key] === 'string') {
675+
return sort[i].direction === 'desc' ?
676+
b[key].localeCompare(a[key]) :
677+
a[key].localeCompare(b[key]);
678+
} else { // Assuming numeric or other comparable types
679+
return sort[i].direction === 'desc' ?
680+
(b[key] - a[key]) :
681+
(a[key] - b[key]);
682+
}
683+
}
684+
}
685+
return 0;
686+
});
687+
}
688+
689+
function sortDataOld(data, sort) {
658690
if (!Array.isArray(sort))
659691
sort = [sort]
660692
for (let i = 0; i < sort.length; i++) {

0 commit comments

Comments
 (0)