// Underscore
          _.sortBy(fruits, 'name');
          // => [{name:"apple", amount: 4}, {name:"banana", amount: 2}, {name:"mango", amount: 1}, {name:"pineapple", amount: 2}]

          // Lodash
          _.orderBy(fruits, ['name'],['asc']);
          // => [{name:"apple", amount: 4}, {name:"banana", amount: 2}, {name:"mango", amount: 1}, {name:"pineapple", amount: 2}]

          // Native
          const sortBy = (key) => {
            return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0);
          };

          fruits.sort(sortBy("name"));
          // => [{name:"apple", amount: 4}, {name:"banana", amount: 2}, {name:"mango", amount: 1}, {name:"pineapple", amount: 2}]
        

Underscore

sortBy

_.sortBy(list, iteratee, [context])

Возвращает копию списка, отсортированную по возрастанию значений, которые возвращает функция iteratee, вызываемая на каждом элементе списка. Параметр iteratee может также быть названием поля, по которому должна вестить сортировка (например length).

          _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
          => [5, 4, 6, 3, 1, 2]

          var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
          _.sortBy(stooges, 'name');
          => [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}];
        

Lo-Dash

sortBy

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

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).

Arguments

  1. collection (Array|Object): The collection to iterate over.
  2. [iteratees=[_.identity]] (...(Function|Function[])): The iteratees to sort by.
          var users = [
            { 'user': 'fred',   'age': 48 },
            { 'user': 'barney', 'age': 36 },
            { 'user': 'fred',   'age': 40 },
            { 'user': 'barney', 'age': 34 }
          ];

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

          _.sortBy(users, ['user', 'age']);
          // => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]
        
        `_.sortBy` with `callback`:
        lodash.min x 154,049 ops/sec ±1.59% (86 runs sampled)
        underscore-min x 104,836 ops/sec ±7.55% (69 runs sampled)
        lodash.min is 56% faster.

        `_.sortBy` with `callback` and `thisArg` (slow path):
        lodash.min x 125,884 ops/sec ±1.77% (81 runs sampled)
        underscore-min x 62,843 ops/sec ±3.37% (81 runs sampled)
        lodash.min is 103% faster.

        `_.sortBy` with `property` name:
        lodash.min x 159,436 ops/sec ±0.60% (84 runs sampled)
        underscore-min x 66,486 ops/sec ±2.94% (84 runs sampled)
        lodash.min is 145% faster.
      
На главную