I have an application which creates and manages activities. I use Knockout.js to hold activities in observable array. Whenever new activity is created it is inserted into an array. One of the activity properties is date. I want to order activities by date after creating new one, to display it properly in UI. This is a function I use for it:
self.Activities.unshift(activity);
self.Activities.sort(function(a, b) {
var dateA = new Date(a.date() + " 00:00:00");
var dateB = new Date(b.date() + " 00:00:00");
return dateA > dateB;
});
And it works perfectly in Firefox (v 16.0.2) but doesn't work in Chrome (v 23.0.1...), Safari or IE
Why? What is the workaround? If any?
The comparer function that you pass needs to sort
needs to return a number. Some browsers are forgiving and work with a boolean.
Generally you would return -1 or 1. Something like:
return dateA > dateB ? 1 : -1;
I was using a wrong Date format. For some reason Chrome doesn't like: d/MM/yyyy, when I used yyyy/MM/d everything works fine
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With