Sunday, 18 August 2013

Why this compareFunction is not working?

Why this compareFunction is not working?

I have an object with the next JSON structure:
{
"matter": [
{
"title": "Systems",
"date": "23/08/2010",
"score": 5
},
....
]
}
I want to sort this variable with the sort() function. I can do it using
the score field, but I can't sort it using the date field. This is what
I'm using:
$.getJSON('js/data.json', function(data) {
// data now contains one node with all the matters
$.each(data, function(key, val) {
// val now contains all the matters in their nodes
val.sort(function (a,b) {
return
parseInt(a.date.substring(6,10)+a.date.substring(3,5)+a.date.substring(0,2))
-
parseInt(b.date.substring(6,10)+b.date.substring(3,5)+b.date.substring(0,2));
});
// Here I get the same array not sorted!
}
}
Both parseInt() functions returns an integer with this format:
if date=="23/08/2010" => 20100823
I used alerts to check if I'm splitting correctly the date but that is
fine. Anyway, I can sort the array.
What I'm doing wrong?
I'm testing the code using this JSON file.

No comments:

Post a Comment