Collect.js sortKeys() Method
Last Updated :
09 Dec, 2020
The sortKeys() method is used to sort the collection elements by the given keys of the underlying associative array.
Syntax:
collect(array).sortKeys(key)
Parameters: The collect() method takes one argument that is converted into the collection and then sortKeys() method is applied on it. The sortKeys() method holds the key as parameter.
Return Value: This method returns sort the collection elements by the given keys of the underlying associative array.
Below example illustrate the sortKeys() method in collect.js:
Example 1:
Javascript
const collect = require( 'collect.js' );
let obj = ({
name: 'Rahul' ,
dob: '25-10-96' ,
section: 'A' ,
score: 98
});
const collection = collect(obj);
const sorted = collection.sortKeys();
console.log(sorted.all());
|
Output:
{ dob: '25-10-96', name: 'Rahul', score: 98, section: 'A' }
Example 2:
Javascript
const collect = require( 'collect.js' );
let obj = ({
5: "Welcome" ,
1: "GeeksforGeeks" ,
8: "to" ,
4: "Computer" ,
9: "Science" ,
10: "Portal"
});
const collection = collect(obj);
const sorted = collection.sortKeys();
console.log(sorted.all());
|
Output:
{
'1': 'GeeksforGeeks',
'4': 'Computer',
'5': 'Welcome',
'8': 'to',
'9': 'Science',
'10': 'Portal'
}
Share your thoughts in the comments
Please Login to comment...