Open In App

How to sort a collection ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to sort the items in a collection. The sort() method is used for this purpose. A collection takes the input of an array containing numbers, strings, or objects. 

Syntax:

collect(array).sort()

Parameters: It takes an array as a parameter that is converted into a collection and then it is sorted according to the values of its elements.

We can sort a collection by using the Collect.js methods in two ways, both methods are mentioned and described below:

  • Collect.js all() Method
  • Collect.js sortByDesc() Method

Collect.js all() Method

The all() method returns all the elements represented by a collection in an underlying array. The JavaScript array is first transformed into a collection and then the function is applied to the collection.

Syntax:

sorted_collection.all()

Example 1: Here collect = require(&#x2018collect.js&#x2019) is used to import the collect.js library into the file.

Javascript




const collect = require('collect.js');
const arr = [8, 2, 14, 26, 21, 17, 19]
const collection = collect(arr);
 
const sorted = collection.sort();
 
console.log(sorted.all());


Output:

[2, 8, 14, 17, 19, 21, 26]

Example 2: We can also sort a collection in descending order using some algorithms. We can pass a callback function for this purpose.

Javascript




const collect = require('collect.js');
const arr = [8, 2, 14, 26, 21, 17, 19]
const collection = collect(arr);
 
const descending_sort = collection.sort((a, b) => b - a);
 
console.log(descending_sort.all());


Output:

[26, 21, 19, 17, 14, 8, 2]

JavaScript sortByDesc() Method

If elements are Objects, then we can sort using the sortByDesc() method that sorts a collection of elements in descending order using a key as the parameter.

Syntax:

collect(array).sortByDesc(key)

Parameters:

  • array: It takes the array as input, which is to be converted to a collection
  • key: It is the parameter according to which the elements are to be sorted in descending order

Example 1:

Javascript




const collect = require('collect.js');
const arr = ['Science', 'Welcome',
    'Computer', 'GeeksforGeeks']
 
const collection = collect(arr);
 
const descending_sort = collection.sortByDesc();
console.log(descending_sort);


Output:

Collection { items: [ 'Welcome', 'Science', 'GeeksforGeeks', 'Computer' ] }

Example 2: We can also pass an array of objects and use a property of objects as a key to sort the elements of the collection.

Javascript




const collect = require('collect.js');
 
let Employee = [
    {
        name: 'Shyam',
        id: 21
    },
    {
        name: 'Aditya',
        id: 48
    },
    {
        name: 'Chris',
        id: 12
    }
];
const collection = collect(Employee);
 
const sort_id = collection.sortByDesc('id')
console.log(sort_id);


Output:

Collection {
items: [
{ name: 'Aditya', id: 48 },
{ name: 'Shyam', id: 21 },
{ name: 'Chris', id: 12 }
]
}


Last Updated : 11 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads