Skip to content

sortBy

js
_.sortBy(collection, [iteratees=[_.identity]])

创建一个元素数组,按每个迭代器运行集合中每个元素的结果按升序排序。此方法执行稳定排序,即保留相等元素的原始排序顺序。迭代器使用一个参数调用:(value)。

¥Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru 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).

新增于

¥Since

0.1.0

参数

¥Arguments

  1. collection (数组|对象):要迭代的集合。

    ¥collection (Array|Object): The collection to iterate over.

  2. [iteratees=[_.identity]] (...(函数|函数[])):用于排序的迭代器。

    ¥[iteratees=[_.identity]] (...(Function|Function[])): The iteratees to sort by.

返回

¥Returns

(数组):返回新的排序数组。

¥(Array): Returns the new sorted array.

示例

¥Example

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

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

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

Lodash v4.17 中文网 - 粤ICP备13048890号