Languages
[Edit]
EN

JavaScript - sort array of objects by ISO 8601 date string property value

0 points
Created by:
lena
714

In this article, we would like to show you how to sort array of objects by ISO 8601 date string property value in JavaScript.

Quick solution:

array.sort(function(a, b) {
    return a.dateProperty.localeCompare(b.dateProperty);
});

Note: it works only when we use ISO 8601 date strings in UTC variant (ended with Z).

or:

array.sort(function(a, b) {
    var aDate = new Date(a.birthday);
    var bDate = new Date(b.birthday);
    return aDate.getTime() - bDate.getTime();
});

Note: it shoulod be used when ISO 8601 date strings may contain timezone offset (ended with +XX:YY or -XX:YY).

 

Practical example

In this example, we sort an array of objects by date property value using the custom compare function as an argument of the sort() method.

1. Ascending order

In this example, we use an embedded String localeCompare() method.

// ONLINE-RUNNER:browser;

var array = [
    { name: 'Ann',  birthday: '2000-01-02T00:00:00Z' },
    { name: 'Tom',  birthday: '2000-01-01T00:00:00Z' },
    { name: 'Mark', birthday: '2000-01-03T00:00:00Z' }
];

array.sort(function(a, b) {
    return a.birthday.localeCompare(b.birthday);
});

console.log(JSON.stringify(array, null, 4));

Comparison using Date objects:

// ONLINE-RUNNER:browser;

var array = [
    { name: 'Ann',  birthday: '2000-01-02T00:00:00Z' },
    { name: 'Tom',  birthday: '2000-01-01T00:00:00Z' },
    { name: 'Mark', birthday: '2000-01-03T00:00:00Z' }
];

array.sort(function(a, b) {
    var aDate = new Date(a.birthday);
    var bDate = new Date(b.birthday);
    return aDate.getTime() - bDate.getTime();
});

console.log(JSON.stringify(array, null, 4));

2. Descending order

// ONLINE-RUNNER:browser;

var array = [
    { name: 'Ann',  birthday: '2000-01-02T00:00:00Z' },
    { name: 'Tom',  birthday: '2000-01-01T00:00:00Z' },
    { name: 'Mark', birthday: '2000-01-03T00:00:00Z' }
];

array.sort(function(a, b) {
    return b.birthday.localeCompare(a.birthday);
});

console.log(JSON.stringify(array, null, 4));

or:

// ONLINE-RUNNER:browser;

var array = [
    { name: 'Ann',  birthday: '2000-01-02T00:00:00Z' },
    { name: 'Tom',  birthday: '2000-01-01T00:00:00Z' },
    { name: 'Mark', birthday: '2000-01-03T00:00:00Z' }
];

array.sort(function(a, b) {
    var aDate = new Date(a.birthday);
    var bDate = new Date(b.birthday);
    return bDate.getTime() - aDate.getTime();
});

console.log(JSON.stringify(array, null, 4));

 

See also

  1. JavaScript - sort array of objects by date property value

References

  1. Array.prototype.sort() - JavaScript | MDN
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join