javascript - How to sort json data firstly by data and then by time? -


i have json data similar below. trying sort using below code

function comp(a, b) {      return new date(a.event_date) - new date(b.event_date); }  data=data.sort(comp)     

but problem 2 events can on same dates on different times element in json data called event_time can seen on same json data.

how sort such firstly sorts event_date , if these equal, sorts respective times ?

thanks in advance?

{     id: "xxxx",     feed_id: "yyyy",     title: "abcd ",     detail: "efgh.",     event_date: "tue, 26 may 2015 00:00:00 +1000",     event_time: "6:30pm",     date: "thu, 23 apr 2015 23:05:04 +1000",     expires_at: 1432634400,     end_time: "8:00pm",     timestamp: 1429794304 }, {     id: "xxxx",     feed_id: "yyyy",     title: "efgh",     detail: "efgh.",     event_date: "tue, 26 may 2015 00:00:00 +1000",     event_time: "4:30pm",     date: "thu, 23 apr 2015 23:05:04 +1000",     expires_at: 1432634400,     end_time: "8:00pm",     timestamp: 1429794304 },    {     id: "xxxx",     feed_id: "yyyy",     title: "ijkl",     detail: "efgh.",     event_date: "tue, 27 may 2015 00:00:00 +1000",     event_time: "1:30pm",     date: "thu, 23 apr 2015 23:05:04 +1000",     expires_at: 1432634400,     end_time: "8:00pm",     timestamp: 1429794304 } 

if subtracting 2 dates results in 0 (no difference), include time in equation. like:

var result = document.queryselector('#result');  result.textcontent =      json.stringify(getdata().sort(sortdatetime), null, ' ') +     '\n\n**using sortdatetime2\n'+     json.stringify(getdata().sort(sortdatetime2), null, ' ');        function sortdatetime(a, b) {    var = new date(a.event_date);    var b = new date(b.event_date);    if (a - b == 0) {      // no difference, include event_time      var ta = a.event_time.split(':');      var tb = b.event_time.split(':');      a.sethours( /pm/i.test(ta[1]) ? +ta[0]+12 : ta[0] );      a.setminutes(+ta[1].replace(/[a-z]/gi, ''));      b.sethours(/pm/i.test(tb[1]) ? +tb[0]+12 : tb[0]);      b.setminutes(+tb[1].replace(/[a-z]/gi, ''));    }    return - b;  }    // sorting on date/time using settime helper  function sortdatetime2(a, b) {    return settime(new date(a.event_date), a.event_time) -            settime(new date(b.event_date), b.event_time);  }    // bonus: helper set time time string  function settime(thisdate, timestr) {    return new date(             [ [ thisdate.getfullyear(),                  thisdate.getmonth()+1,                  thisdate.getdate()                ].join('/'),               timestr.replace(/(a|p)/, function (m) {return ' ' + m;} ) ]               .join(' ')    );  }    function getdata() {    return [{       id: "xxxx",       feed_id: "yyyy",       title: "abcd ",       detail: "efgh.",       event_date: "tue, 26 may 2015 00:00:00 +1000",       event_time: "6:30pm",       date: "thu, 23 apr 2015 23:05:04 +1000",       expires_at: 1432634400,       end_time: "8:00pm",       timestamp: 1429794304      },      {       id: "xxxx",       feed_id: "yyyy",       title: "abcd ",       detail: "efgh.",       event_date: "tue, 26 may 2015 00:00:00 +1000",       event_time: "4:30pm",       date: "thu, 23 apr 2015 23:05:04 +1000",       expires_at: 1432634400,       end_time: "8:00pm",       timestamp: 1429794304      },    {       id: "xxxx",       feed_id: "yyyy",       title: "abcd ",       detail: "efgh.",       event_date: "tue, 27 may 2015 00:00:00 +1000",       event_time: "1:30pm",       date: "thu, 23 apr 2015 23:05:04 +1000",       expires_at: 1432634400,       end_time: "8:00pm",       timestamp: 1429794304      }    ];  }
<pre id="result"></pre>


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -