All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Sort Object By Key

Last Updated : Mar 12, 2024

JavaScript Sort Object By Key

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>
  1. Specifying the HTML version of a file is done by writing <!DOCTYPE html> in the HTML code.
  2. The <HTML> tag, indicating the root of an HTML document, should be included and closed with the </html> tag at the end.
  3. Metadata for the HTML file is contained within the <head> tag, which is terminated with the </head> tag.
  4. The HTML document's title is defined using the <title> tag, followed by the </title> tag.
  5. Styling for the HTML page will be achieved by utilizing an external CSS file.
  6. CSS can be added using the <style> tag.
  7. JavaScript can be written within the <script> tag, and it should be closed with the </script> tag.
  8. Now create a JavaScript object named ‘employee’ using const.
  9. Add some keys and values such as ‘name’, ‘age’, and ’gender’ into it. We are going to sort this object by age.
  10. First using Console.table() for displaying the object before sorting
  11. Now use the sort() method and write a function into it.
  12. 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.
  13. 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.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪