10

If I have a list of object:

var objectList= LIST_OF_OBJECT;

each object in the list contains three attributes: "name", "date","gender"

How to sort the objects in the list by "date" attribute ascending order?

(the "date" attribute contain string value like "2002-08-29 21:15:31+0500")

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Leem
  • 17,220
  • 36
  • 109
  • 159

4 Answers4

29

If your objects have the date information within a String field:

yourArray.sort(function(a, b) { return new Date(a.date) - new Date(b.date) })

or, if they have it within a Date field:

yourArray.sort(function(a, b) { return a.date - b.date })
turdus-merula
  • 8,546
  • 8
  • 38
  • 50
  • 2
    Congratulations for having the only sane answer here. – Tom Jul 31 '17 at 16:20
  • 1
    You don't even need `new Date` at all if they are [valid date strings](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript) in the same time zone. – str Dec 10 '18 at 12:28
  • A small modification on this great answer for anyone using typescript: https://stackoverflow.com/a/40248661/6121634. – ericArbour Jul 04 '23 at 16:08
11

The Array.sort method accepts a sort function, which accepts two elements as arguments, and should return:

  • < 0 if the first is less than the second
  • 0 if the first is equal to the second
  • > 0 if the first is greater than the second.

.

objectList.sort(function (a, b) {
    var key1 = a.date;
    var key2 = b.date;

    if (key1 < key2) {
        return -1;
    } else if (key1 == key2) {
        return 0;
    } else {
        return 1;
    }
});

You're lucky that, in the date format you've provided, a date that is before another date is also < than the date when using string comparisons. If this wasn't the case, you'd have to convert the string to a date first:

objectList.sort(function (a, b) {
    var key1 = new Date(a.date);
    var key2 = new Date(b.date);

    if (key1 < key2) {
        return -1;
    } else if (key1 == key2) {
        return 0;
    } else {
        return 1;
    }
});
Matt
  • 74,352
  • 26
  • 153
  • 180
  • 1
    I would start by defining a new field in each value with the date already parsed for efficiency. – Serabe Sep 22 '11 at 10:11
  • How about if it is not a list of objects, but a object which contain objects, and I would like to sort the objects inside a object? – Leem Sep 22 '11 at 10:13
  • @Leem: There is nothing in any standards which describes what order object members should be enumerated in (citation needed). You should convert the object to an array. – Matt Sep 22 '11 at 10:16
  • If `date` field always contains value as "2002-08-29 21:15:31+0500" and always have "+0500", then posible to compare they for sorting without parsing into `Date` object. – Andrew D. Sep 22 '11 at 10:20
3
yourArray.sort(function(a, b) { 
 a = new Date(a.date);
 b = new Date(b.date);
 return a >b ? -1 : a < b ? 1 : 0;
})
RredCat
  • 5,259
  • 5
  • 60
  • 100
sd.n
  • 51
  • 2
-1

You can try:

var a =[-1,0,5,4,3,2,6,1,1];
var b = totzarrange(a)
console.log(b);
kldjlkd
  • 13
  • 4
as sdfas
  • 1
  • 3