3

I have the following code which is sorting a list of javascript objects in an array based on their date. The data is coming from an XML file. The date is formatted as follows: MM-DD-YYYY

concert=new Object();
concert.performer=performerName;
concert.date=concertDate;
concerts[0]=concert; //adding to array in a for loop

So at this stage I have a load of concert objects in my concerts array. I then go to sort it and output it to a table:

sortedConcerts = concerts.sort(sortConcerts);

function sortConcerts(a, b){
var firstConcert=new Date(a.date);
var secondConcert=new Date(b.date);
return firstConcert-secondConcert;
}

I then have the new sorted array which I print out using a table or whatever.

My problem is that this works fine in IE and Chrome, but not in Firefox... what does Firefox not like?

Markus
  • 33
  • 1
  • 4
  • 2
    Look at the Javascript console. Does it complain? – Jon Dec 13 '11 at 14:35
  • 1
    What exactly do those "concertDate" values look like? In your comparator routine there is no need to create new "Date" instances unless the values are strings, and if they're strings they must be in the correct format for constructing "Date" instances. – Pointy Dec 13 '11 at 14:35
  • What exactly isn't working, the sorting? What error messages (if any) are you getting? – Jeffrey Sweeney Dec 13 '11 at 14:36
  • I am getting no error messages or any grief from the console, it simply does not sort in Firefox but it does in IE. My date strings are in the correct format. – Markus Dec 13 '11 at 14:38
  • Is it possible to set up a [jsFiddle](http://jsfiddle.net/) example? – jabclab Dec 13 '11 at 14:46
  • That date format ("MM-DD-YYYY") will not work with Firefox. – Pointy Dec 13 '11 at 14:50

4 Answers4

2

Your date format ("MM-DD-YYYY") is not valid. Thus your "Date" instances are always identical.

You could flip the strings around and just compare as strings:

function sortConcerts(a, b) {
  function flipDate(d) {
    return d.replace(/(\d\d)-(\d\d)-(\d\d\d\d)/, "$3-$1-$2");
  }

  var d1 = flipDate(a.date), d2 = flipDate(b.date);
  return d1 > d2 ? 1 : d2 > d1 ? -1 : 0;
}

Firefox insists on dates following IETF standards (I think it's from RFC-822 originally).

If there are a zillion concerts, it'd be more efficient to flip the dates around for all the concerts before sorting them.

Pointy
  • 405,095
  • 59
  • 585
  • 614
2

Firefox seems to accept:

new Date("Jan 1 2009");
new Date("January 1 2009");
new Date("1 1 2009");
new Date("1/1/2009");

However using the hyphens gives you an invalid date format, which results in NaN for mathematic operations, (in your case, subtraction);

new Date("1/1/2009") - new Date("1-1-2009"); // NaN in Firefox, 0 in other browsers
new Date("1/1/2009") - new Date("1/1/2009"); // 0 in all browsers.

MDN has an article on valid date formats.

Dennis
  • 32,200
  • 11
  • 64
  • 79
  • I see. I wonder why IE and Chrome don't mind the hyphens. Thank you for your help, I've tested out forward slashes instead and they work. – Markus Dec 13 '11 at 14:55
0

Try to use sortBy from loadesh

const result = sortBy(array, item => item.your.deep.data)          
return (currentDirection === 'asc') ? result : result.reverse()
0

Here is functioning code fiddle

I verified on IE, Chrome & FF.

Sandeep G B
  • 3,957
  • 4
  • 26
  • 43