Knockout sorting method doesn't work in chrome

I have an application that creates and controls actions. I am using Knockout.js to store activity in an observable array. Whenever a new activity is created, it is inserted into the array. One of the properties of an activity is a date. I want to order actions by date after creating a new one in order to display it correctly in the user interface. This is the 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 fine in Firefox (v 16.0.2), but it doesn’t work in Chrome (v 23.0.1 ...), Safari or IE

Why? What is the workaround? If there?

+4
source share
2 answers

The comparison function you pass requires sort to return a number. Some browsers forgive and work with Boolean.

Usually you returned -1 or 1. Something like:

 return dateA > dateB ? 1 : -1; 
+7
source

I used the wrong date format. For some reason, Chrome doesn't like it: d / MM / yyyy, when I used yyyy / MM / d, everything works fine

+1
source

All Articles