CodingSparkles

Sorting an Array (Array of Objects)

JavaScript
2 min read
October 05, 2021

In this article we can learn the sorting an array of objects based on the particular property value.

Input Array

const employeeData = [
    {
        name: "James",
        id: 1010,
        dob: new Date(1995, 5, 10)
    },
    {
        name: "William",
        id: 1001,
        dob: new Date(1989, 10, 5)
    },
    {
        name: "Lucas",
        id: 1030,
        dob: new Date(1997, 6, 3)
    },
    {
        name: "Ava",
        id: 1020,
        dob: new Date(1998, 8, 25)
    },
]

Sorting Method

const getSortedData = (propertyName, originalData) => {
    const sortedData = originalData.sort((data1, data2) => {
        if (data1[propertyName] < data2[propertyName]) {
            return -1;
        } else if (data1[propertyName] > data2[propertyName]) {
            return 1;
        } else {
            return 0;
        }
    });
    return sortedData;
}

Sorting based on name property

getSortedData("name", employeeData);

Output

0: {name: "Ava", id: 1020, dob: Fri Sep 25 1998 00:00:00 GMT+0530 (India Standard Time)}
1: {name: "James", id: 1010, dob: Sat Jun 10 1995 00:00:00 GMT+0530 (India Standard Time)}
2: {name: "Lucas", id: 1030, dob: Thu Jul 03 1997 00:00:00 GMT+0530 (India Standard Time)}
3: {name: "William", id: 1001, dob: Sun Nov 05 1989 00:00:00 GMT+0530 (India Standard Time)}

Sorting based on id property

getSortedData("id", employeeData);

Output

0: {name: "William", id: 1001, dob: Sun Nov 05 1989 00:00:00 GMT+0530 (India Standard Time)}
1: {name: "James", id: 1010, dob: Sat Jun 10 1995 00:00:00 GMT+0530 (India Standard Time)}
2: {name: "Ava", id: 1020, dob: Fri Sep 25 1998 00:00:00 GMT+0530 (India Standard Time)}
3: {name: "Lucas", id: 1030, dob: Thu Jul 03 1997 00:00:00 GMT+0530 (India Standard Time)}

Sorting based on dob property

getSortedData("dob", employeeData);

Output

0: {name: "William", id: 1001, dob: Sun Nov 05 1989 00:00:00 GMT+0530 (India Standard Time)}
1: {name: "James", id: 1010, dob: Sat Jun 10 1995 00:00:00 GMT+0530 (India Standard Time)}
2: {name: "Lucas", id: 1030, dob: Thu Jul 03 1997 00:00:00 GMT+0530 (India Standard Time)}
3: {name: "Ava", id: 1020, dob: Fri Sep 25 1998 00:00:00 GMT+0530 (India Standard Time)}
Share: