Collect.js sort() Method

The sort() method is used to sort the items of the collection.
Syntax:
collect(array).sort()
Parameters: The collect() method takes one argument that is converted into the collection and then sort() method is applied on it.
Return Value: This method return the collection after sorting.
Below example illustrate the sort() method in collect.js:
Example 1:
const collect = require('collect.js'); let arr = [8, 4, 7, 6, 5, 11, 3]; const collection = collect(arr); const sorted = collection.sort(); let result = sorted.all(); console.log(result); |
Output:
[3, 4, 5, 6, 7, 8, 11]
Example 2:
const collect = require('collect.js'); let arr = [8, 4, 7, 6, 5, 11, 3]; const collection = collect(arr); const sorted = collection.sort((a, b) => b - a); let result = sorted.all(); console.log(result); |
Output:
[11, 8, 7, 6, 5, 4, 3]
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!



