Collect.js sortDesc() Method

The sortDesc() method is used to sort the collection in the opposite order as the sort method.
Syntax:
collect.sortDesc()
Parameters: The collect() method takes one argument that is converted into the collection and then sortDesc() method is applied on it.
Return Value: This method returns the collection in descending order.
The below example illustrates the sortDesc() method in collect.js:
Example 1:
Javascript
const collect = require('collect.js'); let arr = [8, 4, 7, 6, 5, 11, 3]; const collection = collect(arr); const sorted = collection.sortDesc(); let result = sorted.all(); console.log(result); |
Output:
[11, 8, 7, 6, 5, 4, 3]
Example 2:
Javascript
const collect = require('collect.js'); let obj = [ { subject: 'English', score: 95, }, { subject: 'math', score: 86, }, { subject: 'science', score: 90, }]; const collection = collect(obj); const sorted = collection.sortDesc('score'); let result = sorted.all(); console.log(result); |
Output:
[
{
subject: 'science',
score: 90,
},
{
subject: 'English',
score: 95,
},
{
subject: 'math',
score: 86,
}
]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!



