VOOZH about

URL: https://www.geeksforgeeks.org/javascript/collect-js-sortkeys-method/

⇱ Collect.js sortKeys() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collect.js sortKeys() Method

Last Updated : 9 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:

Output:

{ dob: '25-10-96', name: 'Rahul', score: 98, section: 'A' }

Example 2:

Output:

{
 '1': 'GeeksforGeeks',
 '4': 'Computer',
 '5': 'Welcome',
 '8': 'to',
 '9': 'Science',
 '10': 'Portal'
}
Comment
Article Tags: