sortByOrder source npm

_.sortByOrder(collection, [iteratees=[_.identity]], [orders])

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, a value is sorted in ascending order if its corresponding order is "asc", and descending if "desc".

Arguments

  1. collection (Array|Object)

    The collection to iterate over.

  2. [iteratees=[_.identity]] (Function[]|Object[]|string[])

    The iteratees to sort by.

  3. [orders] (string[])

    The sort orders of iteratees.

Returns (Array)

Returns the new sorted array.

Example

var resolve = _.partial(_.map, _, _.values);

var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 34 },
  { 'user': 'fred',   'age': 42 },
  { 'user': 'barney', 'age': 36 }
];

// sort by `user` in ascending order and by `age` in descending order
resolve( _.sortByOrder(users, ['user', 'age'], ['asc', 'desc']) );
// => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]