Google News
logo
Collect.js - Interview Questions
Explain sort methods in Collect.js.
sort() : The sort() method sorts the collection:
const collection = collect([5, 3, 1, 2, 4]);

const sorted = collection.sort();

sorted.all();

// [1, 2, 3, 4, 5]
If your sorting needs are more advanced, you may pass a callback to sort with your own algorithm.
const collection = collect([5, 3, 1, 2, 4]);

const sorted = collection.sort((a, b) => b - a);

sorted.all();

// [5, 4, 3, 2, 1]
If you need to sort a collection of nested arrays or objects, see the sortBy and sortByDesc methods.
 

sortBy() : The sortBy() method sorts the collection by the given key. The sorted collection keeps the original array keys, so in this example we'll use the values method to reset the keys to consecutively numbered indexes:
const collection = collect([
  { name: 'Desk', price: 200 },
  { name: 'Chair', price: 100 },
  { name: 'Bookcase', price: 150 },
]);

const sorted = collection.sortBy('price');

sorted.all();

// [
//   { name: 'Chair', price: 100 },
//   { name: 'Bookcase', price: 150 },
//   { name: 'Desk', price: 200 },
// ]
You can use dot notation to sort by nested values
const collection = collect([
  {
    name: 'Desk',
    price: 200,
    manufacturer: {
      name: 'IKEA',
    },
  },
  {
    name: 'Chair',
    price: 100,
    manufacturer: {
      name: 'Herman Miller',
    },
  },
  {
    name: 'Bookcase',
    price: 150,
    manufacturer: {
      name: 'IKEA',
    },
  },
]);

const sorted = collection.sortBy('manufacturer.name');

sorted.all();

// [
//   {
//     name: 'Chair',
//     price: 100,
//     manufacturer: {
//       name: 'Herman Miller',
//     },
//   },
//   {
//     name: 'Desk',
//     price: 200,
//     manufacturer: {
//       name: 'IKEA',
//     },
//   },
//   {
//     name: 'Bookcase',
//     price: 150,
//     manufacturer: {
//       name: 'IKEA',
//     },
//   },
// ]
 
You can also pass your own callback to determine how to sort the collection values:
const collection = collect([
  { name: 'Desk', colors: ['Black', 'Mahogany'] },
  { name: 'Chair', colors: ['Black'] },
  { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] },
]);

const sorted = collection.sortBy((product, key) => product.colors.length);

sorted.all();

// [
//   { name: 'Chair', colors: ['Black'] },
//   { name: 'Desk', colors: ['Black', 'Mahogany'] },
//   { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] },
// ]

sortByDesc()  : This method has the same signature as the sortBy method, but will sort the collection in the opposite order.
 
sortDesc()  : This method will sort the collection in the opposite order as the sort method.
const collection = collect([1, 3, 5, 2, 4]);

const sorted = collection.sortDesc();

sorted.all();

// [5, 4, 3, 2, 1]
Unlike sort, you may not pass a callback to sortDesc. If you wish to use a callback, you should use sort and invert your comparison.
 

sortKeys()
The sortKeys() method sorts the collection by the keys of the underlying associative array:
const collection = collect({
  id: 10,
  first: 'Darwin',
  last: 'Núñez',
});

const sorted = collection.sortKeys();

sorted.all();

// {
//   first: 'Darwin',
//   id: 10,
//   last: 'Núñez',
// }

sortKeysDesc()
This method has the same signature as the sortKeys method, but will sort the collection in the opposite order.
Advertisement