Returns a (stably) sorted copy of an array, ranked in ascending order by the results of running each value through an iteratee. iteratee may also be the string name of the property to sort by (eg. length).
Inspired by _.sortBy
. 😄
Install with npm
$ npm install sort-by-util
Or unpkg
<script src="https://unpkg.com/[email protected]/umd/index.js" />
Check out the unit tests on CodePen.
const sortBy = require('sort-by-util');
const users = [
{ user: 'fred', age: 48 },
{ user: 'barney', age: 36 },
{ user: 'fred', age: 40 },
{ user: 'barney', age: 34 }
];
sortBy(users, 'user');
//=> [{ 'user': 'barney', 'age': 36 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 48 }, { 'user': 'fred', 'age': 40 }]
sortBy(users, ({user}) => user);
//=> [{ 'user': 'barney', 'age': 36 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 48 }, { 'user': 'fred', 'age': 40 }]
Like us a lot? Help others know why you like us! Review this package on pkgreview.dev | ➡ |
---|
Type: array
Default: none
The array to iterate over.
Type: string
or function
or object
Default: none
The property name, function or object to sort by.
MIT