with reference of this question Sort json array
I have the following JSON String using ajax and store the object as an array:
var homes = [
{
"h_id":"3",
"city":"Dallas",
"state":"TX",
"zip":"75201",
"price":"162500",
"start_time":"2011-01-26 08:00:00",
"end_time":"2011-01-26 05:00:00"
},
{
"h_id":"4",
"city":"Bevery Hills",
"state":"CA",
"zip":"90210",
"price":"319250",
"start_time":"2011-01-26 12:00:00",
"end_time":"2011-01-26 05:00:00"
},
{
"h_id":"5",
"city":"New York",
"state":"NY",
"zip":"00010",
"price":"962500",
"start_time":"2011-01-28 08:00:00",
"end_time":"2011-01-26 05:00:00"
}
];
How do I create a function to sort the "start_date" field in ASC and also sort in DESC ordering using only JavaScript?
i used below function suggested by Triptych
var sort_by = function(field, reverse, primer){
reverse = (reverse) ? -1 : 1;
return function(a,b){
a = a[field];
b = b[field];
if (typeof(primer) != 'undefined'){
a = primer(a);
b = primer(b);
}
if (a<b) return reverse * -1;
if (a>b) return reverse * 1;
return 0;
}
}
and apply in below manner
// Sort by start_time
homes.sort(sort_by('start_time', false, function(a){return a.getTime()}));
but not working..:((, give this error
a.getTime is not a function
please tell me where i m doing mistake..
Thanks in advance
Note: sorry for copying same question...