All TalkersCode Topics

Follow TalkersCode On Social Media

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

Sort Array Of Objects JavaScript

Last Updated : Mar 11, 2024

Sort Array Of Objects JavaScript

In this tutorial we will show you the solution of sort array of objects JavaScript, we hope you know how to create and access an array of object, if you don’t know then there are already some articles on our website which helps you to understand in quick learning.

So, without wasting more time let us see the example given below to sort an array of objects with the help of JavaScript.

Step By Step Guide On Sort Array Of Objects JavaScript :-

There are many ways with help of which we can sort an array of objects with the help of JavaScript.

So, let us see the most preferred way which we are going to use below in our codes to sort an array of objects. There are many types to sort arrays like:

  • We can sort array w.r.t the string
  • And we can also sort array w.r.t the number present in the array.
  • At last, we can also sort array w.r.t date at which it is created or updated. (This is for database purposes)

In this article, we are going to sort arrays according to the string present in the array whereas the next methods will be discussed in the next session.

// JavaScript sort array of object
<script>
let data = [
    {
        firstName: 'Amie',
        lastName: 'Schofield',
        age:30,
    },
    {
        firstName: 'Indi',
        lastName: 'Cannon',
        age: 55,
    },
    {
        firstName: 'Samara',
        lastName: 'Schultz',
        age: 41,
    }
];
data.sort((val1, val2) => {
    let one = val1.firstName.toLowerCase(),
        two = val2.firstName.toLowerCase();
    if (one < two) {
        return -1;
    }
    if (one > two) {
        return 1;
    }
    return 0;
});
data.forEach((event) => {
    console.log(`${event.firstName} ${event.lastName}`);
});
</script>
  1. As we here see that we today write only JavaScript codes in our example.
  2. In previous tutorials, we already many times create a full code example in which we first create an HTML form with its basic structure. We use HTML, head, title, body, and script tags inside that structure.
  3. In this example, as we see only, we write our JavaScript codes. The paragraph tag used here is only for display outputs or we can also use console instead of HTML tags. Now, let us understand our examples one by one.
  4. As, here we see that we create an array, the array name given in our codes is data. Inside each value of the array, we create a first name, last name, and age. And in this article, we are going to sort the arrays according to the first name. You can also sort according to the last name but the method remains the same. The sort according to the numbers will be shown in the next session.
  5. Now, inside our codes after creating an array we use the sort function to sort arrays. Then we convert our names into lowercase and compare them. Then our if runs and the data get consoled according to the condition. You can see the output inside the console of the browser.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how to sort an array of an object using JavaScript.

I hope this tutorial on sort array of objects JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪