Skip to content

Returns a sorted copy of an array, ranked in ascending order by the results of running each value through an iteratee

License

Notifications You must be signed in to change notification settings

jonkemp/sort-by-util

Repository files navigation

sort-by-util Build Status

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

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.

Usage

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 Review us on pkgreview.dev

API

sortBy(collection, iteratee)

collection

Type: array
Default: none

The array to iterate over.

iteratee

Type: string or function or object
Default: none

The property name, function or object to sort by.

License

MIT

About

Returns a sorted copy of an array, ranked in ascending order by the results of running each value through an iteratee

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published