2

I wanted to sort an array of objects using js. Here is what I have implemented.

var employees = []
employees[0] = {
    retiredate: "01/12/2014"
}
employees[1] = {
    retiredate: "29/01/2015"
}
employees[2] = {
    retiredate: "05/12/2014"
}
employees[3] = {
    retiredate: "08/12/2014"
}
employees[4] = {
    retiredate: "11/12/2014"
}
employees[5] = {
    retiredate: "14/01/2015"
}
employees[6] = {
    retiredate: "03/12/2014"
}
employees[7] = {
    retiredate: "16/01/2015"
}
employees[8] = {
    retiredate: "19/01/2015"
}
employees[9] = {
    retiredate: "22/01/2015"
}
employees[10] = {
    retiredate: "28/01/2015"
}
employees[11] = {
    retiredate: "23/01/2015"
}
employees[12] = {
        retiredate: "15/01/2015"
    }
    //console.log(periodesSorted);

console.log("START  ... ")
employees.sort(function(a, b) {
    var dateA = new Date(a.retiredate),
        dateB = new Date(b.retiredate)
    return dateA - dateB //sort by date ascending
})

for (ind2 = 0; ind2 < employees.length; ind2++) {
    console.log(employees[ind2].retiredate)
}
console.log("End  ... ")

In firefox the array of retiredate is well sorted.

In chrome, I have the following results

01/12/2014
 15/01/2015
05/12/2014
08/12/2014
11/12/2014
14/01/2015
29/01/2015
16/01/2015
19/01/2015
22/01/2015
28/01/2015
23/01/2015
03/12/2014

The results are not sorted. Where did I go wrong in the codes? Can anyone suggest a fix for this ?

Regards

2
  • You cannot produce a valid date from a string in dd/mm/yyyy format; 03/12/2014 would result in March 12th, 23/01/2015 is invalid. Use mm/dd/yyyy format. Commented Jan 15, 2015 at 12:02
  • You could sort ist by transform the dates to an integer, which represents the days between 1.1.1970 and the actual date - and then let a sort algorithm do the rest - to then transform them back Commented Jan 15, 2015 at 12:05

4 Answers 4

1

You can use

employees.sort(function (d1, d2) {
  function parseDate(str) {
    var parts = str.match(/(\d+)/g);
    // assumes M/D/Y date format
    return new Date(parts[2], parts[0]-1, parts[1]); // months are 0-based
  }
  return parseDate(d1.retiredate) - parseDate(d2.retiredate);
});

Here Is example Link

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of this:

new Date(a.retiredate)

Make it like this to convert dd/mm/yyyy to yyyy-mm-dd before creating the date:

new Date(a.retiredate.replace( /(\d{2})\/(\d{2})\/(\d{4})/, "$3-$2-$1") );

Working JSFiddle: http://jsfiddle.net/inanda/csz56b0q/1/

1 Comment

I suggest instead to replace with $3-$2-$1 as that makes it an ISO 8601 format, which is documented to be supported. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

In our case you need convert date to format mm/dd/yyyy, you can do it like this

employees.sort(function(a, b) {
  var toDate = function (date) {
    var res = date.split('/');
    return new Date(res[2], res[1], res[0]).getTime();

    // getTime returns the number of milliseconds
    // or you can use + before new Date..
  };

  return toDate(a.retiredate) - toDate(b.retiredate);
})

Example

Comments

0

Personally I would avoid all the re-parsing & date construction (84 times in your sample);

// prepare the data by appending a date from a reformatted string
for (var i = 0; i < employees.length; i++) {
    var dateString = employees[i].retiredate;
    employees[i]._date = new Date(dateString.substr(6, 4) + "-" + dateString.substr(3, 2) + "-" + dateString.substr(0, 2));
}

// sort without conversion
employees.sort(function(a, b) {
    return a._date - b._date;
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.