sort-by-util
Version:
Returns a sorted copy of an array, ranked in ascending order by the results of running each value through an iteratee.
27 lines (20 loc) • 572 B
JavaScript
const pluck = require('pluck-util');
const { cb, map } = require('@jonkemp/package-utils');
const sortBy = (obj, iteratee, context) => {
let index = 0;
iteratee = cb(iteratee, context);
return pluck(map(obj, (value, key, list) => ({
value,
index: index++,
criteria: iteratee(value, key, list)
})).sort((left, right) => {
const a = left.criteria;
const b = right.criteria;
if (a !== b) {
if (a > b || a === void 0) return 1;
if (a < b || b === void 0) return -1;
}
return left.index - right.index;
}), 'value');
};
module.exports = sortBy;