- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Let’s say we have the following array −
var studentDetails = [ [89, "John"], [78, "John"], [94, "John"], [47, "John"], [33, "John"] ];
And we need to sort the array on the basis of the first item i.e. 89, 78, 94, etc. For this, use sort().
Following is the code −
var studentDetails = [ [89, "John"], [78, "John"], [94, "John"], [47, "John"], [33, "John"] ]; studentDetails.sort((first, second) => second - first) console.log(studentDetails);
To run the above program, you need to use the following command −
Here, my file name is demo293.js.
This will produce the following output on console −
- How to move specific item in array list to the first item in Java?
- Python program to sort a list of tuples by second Item