0

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...

Community
  • 1
  • 1
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • 1. You don't have a JSON object. It is a JavaScript object. It is just JavaScript **object literal notation**. See also: http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json . 2. If you want to sort for `start_date`, why are you sorting for `city`? A string doesn't have a `getTime` method. – Felix Kling Dec 24 '10 at 10:46
  • @Flexi that was typo, i correct that, Thanks – xkeshav Dec 24 '10 at 10:47
  • If your dates are all in that format, then you simply can sort by normal string comparison. – RoToRa Dec 24 '10 at 11:06
  • @RoToRA Yes all dates are in same format, what do i do then? – xkeshav Dec 24 '10 at 11:08
  • 1
    Drop the primer: `homes.sort(sort_by('start_time', false));` – RoToRa Dec 24 '10 at 11:49

3 Answers3

1

As I said in the comment, a string does not have a getTime() method. Only Date objects have. Therefore you have to convert the date string into a Date object. Change your function to:

function(a){return (new Date(a)).getTime()}

Now, there are various implementations of Date and I think only the newer ones support parsing of this kind of date string (not sure though). Maybe you have to parse the string first and pass the single values to Date:

function(a){
    var parts = a.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
    var date = new Date(parts[1], parts[2], parts[3], parts[4], parts[5], parts[6]);
    return date.getTime()}
}

Reference: Date

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • @diEcho: Passing a custom sort function is the only way. And if you are going to use the function from your post then it is like that. Why has it to be short? It is not really long ;) – Felix Kling Dec 24 '10 at 11:46
1

(Combining my comments to a proper answer)

If your dates are all in that format, then you simply can sort by normal string comparison. In this case it means you can just drop the primer:

homes.sort(sort_by('start_time', false));
RoToRa
  • 37,635
  • 12
  • 69
  • 105
0

For ascending sort:

var sortAscending = function(a, b) {
  var aDate = new Date(a["start_time"]);
  var bDate = new Date(b["start_time"]);
  return aDate.getTime() - bDate.getTime();
}

homes.sort(sortAscending)

For descending sort:

var sortDescending = function(a, b) { return -1 * sortAscending(a, b); }

homes.sort(sortDescending);

This is also descending sort:

var sortDescending = function(a, b) { return sortAscending(b, a); }

As others have mentioned, the string representation of your dates is lexicographically sortable, which means that the whole date parsing step can be avoided. Using String.localeCompare the sortAscending function can be written as:

var sortAscending = function(a, b) {
  return a["start_time"].localeCompare(b["start_time"]);
}
Theo
  • 131,503
  • 21
  • 160
  • 205
  • `sortDescending` is not working , both time only Ascending sorting occurs – xkeshav Dec 24 '10 at 11:06
  • Sorry, had written "start_date" instead of "start_time", should work now. Also added another example of how to do descending sort. – Theo Dec 24 '10 at 11:28