Order By In JavaScript
Order by is a generic way of doing sorting on arrays of objects. It supports multiple properties and specifying the order. Here is an example of this in _.countBy:
const cars = [ { id: 'miata', hp: 155, weight: 2400 }, { id: '4c', hp: 237, weight: 2465 }, { id: 'elice', hp: 217, weight: 2000 } ]; const users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 36 } ]; console.log('Order By (Using Lodash):'); const result1 = _.orderBy(cars, 'hp', 'desc'); console.log(result1); const result2 = _.orderBy(users, ['user', 'age'], ['asc', 'desc']); console.log(result2);
A custom order by simply uses the JS sort and iterates over the properties in the sort callback. The following handles only numbers and strings to make the example clearer:
function orderBy(list, properties, orders) { properties = Array.isArray(properties) ? properties : [properties]; orders = Array.isArray(orders) ? orders : [orders]; // To keep the example simple, this only sorts strings and numbers list.sort((a, b) => { let result; for (let index = 0; index < properties.length; index += 1) { let one = a, two = b; if (orders[index] === 'desc') { [one, two] = [two, one]; } const value1 = one[properties[index]]; if (typeof value1 === 'string') { result = String(value1).localeCompare(two[properties[index]]); } else if (typeof value1 === 'number') { result = value1 - two[properties[index]]; } // Only proceed to deeper comparisons if the current results are equal if (result !== 0) { return result; } } return result; }); return list; }
Extending it just involves adding more checks around the typeof part.
Github Location https://github.com/Jacob-Friesen/obscurejs/blob/master/2018/orderBy.js











