Created
January 24, 2022 14:55
-
-
Save xettri/e8c143841084fcae2d5459ece2d152a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ds = function (p) { | |
let s = 1; | |
if (p[0] === '-') { | |
s = -1; | |
p = p.substr(1); | |
} | |
return function (a, b) { | |
let r = 0; | |
if (typeof a[p] === 'string' || typeof b[p] === 'string') { | |
let _a = String(a[p]); | |
let _b = String(b[p]); | |
for (let i = 0; i < _a.length; i++) { | |
if (i > _b.length) { | |
break; | |
} | |
if (_a === _b) continue; | |
r = _a < _b ? -1 : 1; | |
break; | |
} | |
if (r !== 0 && _a.length !== _b.length) { | |
r = _a.length < _b.length ? -1 : 1; | |
} | |
return r; | |
} | |
r = a[p] < b[p] ? -1 : a[p] > b[p] ? 1 : 0; | |
return r * s; | |
}; | |
}; | |
/** | |
* @example | |
* [{...},{...}].sort(obsort('field')); | |
* [{...},{...}].sort(obsort('field1','field2')); | |
*/ | |
const obsort = function () { | |
var a = arguments; | |
return function (o1, o2) { | |
var i = 0, | |
r = 0, | |
n = a.length; | |
while (r === 0 && i < n) { | |
r = ds(a[i])(o1, o2); | |
i++; | |
} | |
return r; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment