Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to sort an array of objects with each object containing

var recent = [{id: "123",age :12,start: "10/17/13 13:07"} , {id: "13",age :62,start: "07/30/13 16:30"}];

Date format is: mm/dd/yy hh:mm

I want to sort in order of date with the most recent first. If date is same it should be sorted on time.

I tried out the below sort function. But it is not working.

recent.sort(function(a,b))
{
    a = new Date(a.start);
    b = new Date(b.start);
    return a-b;
});

Also how should I iterate through the objects for sorting? Something like:

for (var i = 0; i < recent.length; i++)
    {
        recent[i].start.sort(function (a, b)
        {
            a = new Date(a.start);
            b = new Date(b.start);
            return a-b; 
        } );
    }

There can be any number of objects in the array.

share|improve this question
    
Your recent literal is wrong. –  dystroy Oct 17 '13 at 15:14
1  
First block is not valid javascript –  SheetJS Oct 17 '13 at 15:15
    
recent is the name of my array of objects. Can you please elaborate? –  Anthea Oct 17 '13 at 15:16
2  
You need quotes around the date/time. –  Barmar Oct 17 '13 at 15:17
    
You also have an extra parenthesis after the function argument list. Your sort works when I fix that. Is it in the original code, or just a copying error? –  Barmar Oct 17 '13 at 15:22

2 Answers 2

up vote 1 down vote accepted

As a been pointed out in the comments the definition of recent isnt correct javascript.

But assuming the dates are strings:

var recent = [
    {id: 123,age :12,start: "10/17/13 13:07"}, 
    {id: 13,age :62,start: "07/30/13 16:30"}
];

then sort like this:

recent.sort(function(a,b) { 
    return new Date(a.start).getTime() - new Date(b.start).getTime() 
});

More details on sort function from W3Schools

share|improve this answer
    
You can find some useful answers to this topic here: Sort Javascript Object Array By Date –  jherax Nov 5 '14 at 15:35
recent.sort(function(a,b) { return new Date(a.start).getTime() - new Date(b.start).getTime() } );
share|improve this answer
1  
The comparison function is supposed to return negative, 0, or positive depending on the relationship between the arguments, not a boolean. –  Barmar Oct 17 '13 at 15:18
    
Well spotted, I've updated my answer. –  Tom Oct 17 '13 at 15:24
    
Subtracting Date objects apparently returns the same thing as subtracting their getTime() values, although I can't find this requirement in any specification. –  Barmar Oct 17 '13 at 15:31
    
Yes, I've noticed that, although getTime() will always be consistent, and since the OP wants the time component to explicitly be used when sorting, it makes sense to be specific. –  Tom Oct 17 '13 at 15:36
    
A bigger compatibility problem may be that parsing mm/dd/yy format is not portable. –  Barmar Oct 17 '13 at 15:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.