How to use the sortBy function from underscore

Find comprehensive JavaScript underscore.sortBy code examples handpicked from public code repositorys.

underscore.sortBy is a function that sorts a collection by a specified property or function and returns a new sorted collection.

277
278
279
280
281
282
283
284
285
286
    return new Date(parts[1], parts[2] - 1, parts[3]);
});

// Sort the days from newest to oldest
var retVal = [];
var sortedKeys = _.sortBy(_.keys(groupedFiles), function (date) {
    return new Date(date);
}).reverse();

// For each day...
fork icon55
star icon265
watch icon27

+ 116 other calls in file

365
366
367
368
369
370
371
372
373
374
if(!ar.length) return null

//ar = _.shuffle(ar)


ar = _.sortBy(ar, (r) => {return - Number(r.probability || 0) })

var total = _.reduce(ar, function(sum, r){ 
    return sum + Number(r.probability || 0) 
}, 0)
fork icon33
star icon58
watch icon7

+ 2 other calls in file

How does underscore.sortBy work?

underscore.sortBy is a function from the Underscore.js library that takes a collection of values and a function (or a string indicating a property name), and returns a new sorted collection based on the result of applying the function to each element in the collection.

Here's how it works:

  1. The collection is iterated over, and the function is applied to each element in the collection.
  2. The result of the function is then used to sort the collection in ascending order.
  3. If a string is passed instead of a function, _.property is used to create a function that will return the value of the specified property on an object.
  4. If the result of applying the function to two elements is equal, the elements are ordered based on their original position in the collection.

The sorted collection is returned as a new array, leaving the original collection unchanged.

Note that underscore.sortBy is a stable sort, which means that if two elements in the collection have the same sort value, their original order will be preserved in the sorted collection.

underscore.sortBy is useful in a wide range of scenarios where you need to sort a collection of data based on a specific property or function. It's often used in functional programming-style code, as well as in data visualization, user interface development, and other contexts.

237
238
239
240
241
242
243
244
245
246
if (!sort || !sort.length || sort[0] == ',') return items;

let [sort_by, sort_how] = sort;

if (sort_by == "added") {
	items = _.sortBy(items, function (item) {
		return  new Date(item['listed_at'])
	});
}
else if (sort_by == "released"){
fork icon3
star icon10
watch icon4

+ 13 other calls in file

28
29
30
31
32
33
34
35
36
37
});

// getLabs
it('Should give all labs', (done) => {
  labsData.getLabs(helper.userRepo(), (err, data) => {
    const sorted = _.sortBy(data);
    const defaultLabs = _.sortBy(['composeImportedLab', 'composeNoNetworkLab', 'test', 'test2', 'testreadme', 'testreadmedir', 'toEditLab', 'existentLab']);
    expect(err).to.be.null;
    expect(sorted).to.be.eql(defaultLabs);
    done();
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const people = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 20 },
];

const sortedPeople = _.sortBy(people, "age");

console.log(sortedPeople);
// Output:
// [
//   { name: "Charlie", age: 20 },
//   { name: "Alice", age: 25 },
//   { name: "Bob", age: 30 }
// ]

In this example, we start with an array of three objects representing people, each with a name and an age. We pass this array to _.sortBy, along with the string "age", which tells _.sortBy to sort the array based on the age property of each object. After sorting the array, _.sortBy returns a new array with the same objects, but in a different order: first Charlie (age 20), then Alice (age 25), and finally Bob (age 30). Note that _.sortBy does not modify the original array. Instead, it returns a new sorted array, leaving the original array unchanged.

7
8
9
10
11
12
13
14
15
16
if (!id_key) {
  id_key = "_id";
}
if (hit_first) {
  values = docs.getProperty(id_key);
  return _.sortBy(docs, function(doc) {
    var _index;
    _index = ids.indexOf(doc[id_key]);
    if (_index > -1) {
      return _index;
fork icon0
star icon0
watch icon1

1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
  const yourTypes = {
    Pending: 1,
    Approved: 2,
    Unapproved: 3,
  };
  Price = _.sortBy(Price, obj => yourTypes[obj.status]);
}
// if(!queryData.skip) {
//     queryData.skip = 0;
// }
fork icon0
star icon0
watch icon1

+ 3 other calls in file

105
106
107
108
109
110
111
112
113
114
  });

  var alphaSort = function (row) {
    return row[0];
  };
  rows = _.sortBy(rows, alphaSort);

  var Console = require('./console.js').Console;
  return Console.printTwoColumns(rows, options);
};
fork icon0
star icon0
watch icon7