JavaScript Sort Object By Key
Last Updated : Mar 12, 2024
IN - JavaScript | Written & Updated By - Pragati
In this article we will show you the solution of JavaScript sort object by key, in the following example, we will use the JavaScript built-in method Sort() to sort objects by key. First, tell us about the sort() method.
Sort(): In JavaScript, this is an array method. It's used to sort an array by making changes to the original array.
To sort it in ascending or descending order, we must insert a comparison function inside it. The array syntax is used for the sort() method
sort([compareFunction]);
Step By Step Guide On JavaScript Sort Object By Key :-
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> JavaScript sort object by key </title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> <style> body{ background-color: aliceblue; font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; } h1{ color : rgb(95, 194, 95) ; font-size: 25px; font-weight : bolder ; } h3{ color : rgb(95, 194, 95) ; font-size: 20px; } </style> </head> <body> </head> <body> <center> <h1> TALKERSCODE </h1> <h3> JavaScript sort object by key </h3> </center> <script> const Employees = [ {name: "rohan", age: 19, gender: "male"}, {name: "akash", age: 23, gender: "male"}, {name: "priti", age: 21, gender: "female"}, {name: "anjan", age: 26, gender: "male"}, {name: "nil", age: 30, gender: "male"}, {name: "tanu", age: 28, gender: "female"}, {name: "ritu", age: 18, gender: "female"}, ] ; console.table(Employees) ; Employees.sort((s1, s2) => s2.age-s1.age); console.table(Employees) ; </script> </body> </html>
- Specifying the HTML version of a file is done by writing <!DOCTYPE html> in the HTML code.
- The <HTML> tag, indicating the root of an HTML document, should be included and closed with the </html> tag at the end.
- Metadata for the HTML file is contained within the <head> tag, which is terminated with the </head> tag.
- The HTML document's title is defined using the <title> tag, followed by the </title> tag.
- Styling for the HTML page will be achieved by utilizing an external CSS file.
- CSS can be added using the <style> tag.
- JavaScript can be written within the <script> tag, and it should be closed with the </script> tag.
- Now create a JavaScript object named ‘employee’ using const.
- Add some keys and values such as ‘name’, ‘age’, and ’gender’ into it. We are going to sort this object by age.
- First using Console.table() for displaying the object before sorting
- Now use the sort() method and write a function into it.
- By the function, use formal parameters of s1 and s2. That returns the difference of the age of s1 and s2. If it returns negative s1 sorts before s2, if positive s2 sorts before s1 and if returns zero the it will not change.
- Lastly, using console.table() for displaying the object after sorting
Conclusion :-
Lastly, in conclusion, we can say that with this article’s help, we know to sort objects by key using JavaScript.
I hope this article on JavaScript sort object by key helps you and the steps and method mentioned above are easy to follow and implement.