Search


Search something to see results

lodashSortBy

Creates an array of elements, sorted in ascending order by the results of running each element in a collection through each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The iteratees are invoked with one argument: (value).

Type Parameters

  • T

Parameters

  • collection: undefined | null | List<T>

    The collection to iterate over.

  • Optionaliteratees: Many<ListIteratee<T>>[]

    The iteratees to sort by, specified individually or in arrays.

Returns T[]

Returns the new sorted array.

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

_.sortBy(users, function(o) { return o.user; });
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]

_.sortBy(users, ['user', 'age']);
// => objects for [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]

_.sortBy(users, 'user', function(o) {
return Math.floor(o.age / 10);
});
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]

Type Parameters

  • T extends object

Parameters

  • collection: undefined | null | T
  • iteratees: Many<ObjectIteratee<T>>[]

Returns T[keyof T][]

_.sortBy