Open In App

Collect.js sortDesc() Method

Last Updated : 06 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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, 
   }
]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads