1

I have this code:

<script type="text/javascript">
    function abc(objarray) {  
    objarray = objarray.sort(function (a, b) { return new Date(a).getTime() - new Date(b).getTime() });
    alert(objarray);
}
objarray = ["16.08.1993 11:13", "16.08.1994 11:12", "13.08.1994 11:12", "13.08.1996 10:12", "08.08.1996 10:12"];
abc(objarray);
</script>

Date time format: dd.MM.yyyy HH:MM

I want to sort so that I can get the latest date first, but its not working.

LIGHT
  • 5,604
  • 10
  • 35
  • 78
  • 2
    You have `Invalid Date` on `new Date(a).getTime()`. You need parse before compare date – Pavlo Zhukov Apr 25 '17 at 18:36
  • 5
    The date string you're passing is invalid. String value representing a date. The string should be in a format recognized by the Date.parse() method (IETF-compliant RFC 2822 timestamps and also a version of ISO8601). – WilomGfx Apr 25 '17 at 18:39

2 Answers2

2

You need to switch a and b and take another string for comparing, like

1993-08-16 11:13

the ISO 6801 data and time format, wich is comparable with String#localeCompare.

function abc(objarray) {
    objarray = objarray.sort(function(a, b) {
        function getISO(s) {
            return s.replace(/(..).(..).(....) (.....)/, '$3-$2-$1 $4');
        }
        return getISO(b).localeCompare(getISO(a));
    });
}

var objarray = ["16.08.1993 11:13", "16.08.1994 11:12", "13.08.1994 11:12", "13.08.1996 10:12", "08.08.1996 10:12"];
abc(objarray);

console.log(objarray);
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

Try this:

String.prototype.getCorrectDate = function () {
    var date = this.split(' ')[0];
    var hours = this.split(' ')[1];

    var dateSplitted = date.split('.');
    return new Date(dateSplitted[2] + '.' + dateSplitted[1] + '.' + dateSplitted[0] + ' ' + hours);
};

var dates = ["16.08.1993 11:13", "16.08.1994 11:12", "13.08.1994 11:12", "13.08.1996 10:12", "08.08.1996 10:12"];

var sorted = dates.sort(function(a, b) {
    return b.getCorrectDate() - a.getCorrectDate();    
});

alert('First from sorted: '+ sorted[0]);
alert('Last from sorted: '+ sorted[sorted.length - 1]);

https://jsfiddle.net/Lcq6wqhb/

Javascript's native method sort is used to sorting arrays, and we can pass callback function let's say sorting behavior(Sorting an array of JavaScript objects). But before sorting we need to transform date strings to correct format, to be accepted new Date(dateString) as parameter, otherwise it gives error Invalid Date. I'm transorming dd.mm.yyyy hh:MM to yyyy.mm.dd HH:MM using getCorrectDate method

Community
  • 1
  • 1
Arkadi
  • 1,153
  • 1
  • 14
  • 35
  • Please add some explanation of why/how this code helps the OP. This will help provide an answer future viewers can learn from. See [this Meta question and its answers](http://meta.stackoverflow.com/q/256359/215552) for more information. – Heretic Monkey Apr 25 '17 at 18:55
  • 1
    If you're going to parse a string to make a Date, pass the parts directly to the Date constructor (e.g. `return new Date(dateSplitted[2], dateSplitted[1]-1, ...)`), don't make another string for the built-in parser to parse, especially one that is known to fail in at least some browsers (see [*Why does Date.parse give incorrect results?*](http://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results)) – RobG Apr 25 '17 at 22:48