@adaptabletools/adaptable-cjs
Version:
Powerful data-agnostic HTML5 AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements
34 lines (33 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = orderBy;
/**
* This method is like `sortBy` except that it allows specifying the sort
* orders of the iteratees to sort by. If orders is unspecified, all values
* are sorted in ascending order. Otherwise, specify an order of "desc" for
* descending or "asc" for ascending sort order of corresponding values.
* Drop-in replacement for lodash/orderBy.
*/
function orderBy(collection, iteratees, orders) {
const result = [...collection];
const fns = iteratees.map((iteratee) => typeof iteratee === 'function' ? iteratee : (item) => item?.[iteratee]);
result.sort((a, b) => {
for (let i = 0; i < fns.length; i++) {
const valA = fns[i](a);
const valB = fns[i](b);
if (valA !== valB) {
const order = orders?.[i] === 'desc' ? -1 : 1;
if (valA == null)
return 1;
if (valB == null)
return -1;
if (valA < valB)
return -order;
if (valA > valB)
return order;
}
}
return 0;
});
return result;
}