1

UPDATE: Looks like we're headed for a bounty. First time ever for me.

My goal is to adjust this code to get results ordered by date.

CODE SAMPLE

$.when( $.getJSON(url0), $.getJSON(url1), $.getJSON(url2), $.getJSON(url3),$.getJSON(url4), $.getJSON(url5),$.getJSON(url6),$.getJSON(url7) ).done( function() {

    $.each(arguments, function(index, result) {
        var data = result[0];
        utcday =  data[0].createdOn;
        ltrDay = moment.utc(utcday).format("DD MMM YY");

console.log(ltrDay);

    });
});

This code returns the results ordered by the getJSON url I'm calling.

It is the desired behavior in most cases.

However, I now need to return the results ordered by date too for other cases (descending order, newest first).

user1452893
  • 848
  • 1
  • 11
  • 29
  • Related: [Sort Javascript Object Array By Date](http://stackoverflow.com/questions/10123953/sort-javascript-object-array-by-date) – undefined Oct 24 '14 at 23:38
  • @Vohuman Thanks. I tried various approaches and could not get it to work. That's why I'm asking with the specific context. I'm hoping someone can show me a specific code example. – user1452893 Oct 24 '14 at 23:49
  • What is this `moment.utc(utcday).format("DD MMM YY");`? – StackSlave Oct 24 '14 at 23:57
  • Moment is a JS library for time manipulation. – elzi Oct 25 '14 at 00:00
  • @PHPglue I'm doing a lot of stuff with the actual results. The code sample just shows a console.log that returns unordered results. That's there so I can quickly check an answer. Moment is a library that does cool stuff with timestamps. – user1452893 Oct 25 '14 at 00:03
  • Bottom line is you should create an Array of Objects so each Object can be numerically indexed. Loop over the outer Array for each Object to get your `ltrDay` property and store each one into a single Array that maybe that Library takes care of the sorting, or write a function that stores the indexes in an Array, so you can index that Array to get that specific Object out of your Array of Objects. – StackSlave Oct 25 '14 at 00:10

3 Answers3

2

You have the results inside your when, now you just need to sort using a standard sort function.

$.when( $.getJSON(url0), $.getJSON(url1), $.getJSON(url2), $.getJSON(url3),$.getJSON(url4), $.getJSON(url5),$.getJSON(url6),$.getJSON(url7) ).done( function() {
    var results = args = Array.prototype.slice.call(arguments);
    args.sort(function(a, b) {
      return a[0][0].createdOn - b[0][0].createdOn;
    });
    // use sorted args now, args are the results
    console.log(results);

    // assuming a modern browser and using forEach
    results.forEach(function(result) {
      console.log(result[0][0].createdOn);
    });
});

Maybe I'm missing something? But you should be able to use your results which should now be sorted by the createdOn property. I'm assuming your createdOn property is already a date object.

Evil Buck
  • 1,029
  • 7
  • 19
  • Thanks but I still don't get it. Maybe I'm putting your code in the wrong place. Would you be kind enough to match it up to my example? – user1452893 Oct 25 '14 at 17:43
  • createdOn is a timestamp: 'createdOn: 1414179586293' I'm not clear on where to place your block. That is why I'm trying to match up with my example so I can see if it works. – user1452893 Oct 25 '14 at 17:54
  • I ran your code as is above. it returns this: `[Array[3], Array[3], Array[3], Array[3], Array[3], Array[3], Array[3], Array[3]]` with which I have no idea with how to extract the stuff I need. For example, how could I display and see if the createdOn is ordered correctly? – user1452893 Oct 25 '14 at 19:03
  • @user1452893 if you're using chrome or firefox, you should be able to inspect the logging of each array in the console, but I included a forEach loop to print out just the `createdOn` properties. – Evil Buck Oct 25 '14 at 19:43
  • Aha! result[0][0] Thanks. This should get me to the next step. I need to make other adjustments but this looks like it takes care of the ordering bit. – user1452893 Oct 25 '14 at 19:52
  • I was using the index in `$.each(arguments, function(index, result) {' bit in my example to match a name to the url in the getJSON request. Was easy since the order orf the calls was there. Any thoughts on how I could get that bit back? BTW, you're not so "Evil". ;) – user1452893 Oct 25 '14 at 20:07
  • Now worries on the index bit. Used another approach. – user1452893 Oct 25 '14 at 21:50
1

Make sure you echo json_encode($assocArray) the proper JSON, which will go into the data arguments you see below. This way when they are done the variables above $.when() will have the data I believe you want as your arguments Array Like Object.

var u0, u1, u2, u3, u4, u5, u6, u7;
$.when($.getJSON(url0, function(data){u0 = data}), $.getJSON(url1, function(data){u1 = data}), $.getJSON(url2, function(data){u2 = data}), $.getJSON(url3, function(data){u3 = data}), $.getJSON(url4, function(data){u4 = data}), $.getJSON(url5, function(data){u5 = data}), $.getJSON(url6, function(data){u6 = data}), $.getJSON(url7, function(data){u7 = data})).done(function(){
  var arrayOfObjects = [];
  $.each([u0, u1, u2, u3, u4, u5, u6, u7], function(index, result){
    var rz = result[0];
    arrayOfObjects.push({data:rz, utcday:rz.data[0].createdOn, ltrDay:moment.utc(utcday).format('DD MM YY')});
  });
  function youMakeIt(aryObs){
    var dateArray = [];
    $.each(aryObs, function(i, v){
      dateArray.push(v.ltrDay);
    });
    /* now you do your thing with `dateArray` putting the array of indexes in
       an `indexes` variable to get the `arrayOfObjects` in the order you like
       Next its like */
    var solution = [];
    $.each(indexes, function(i, v){
      solution.push(dateArray[v]);
    });
    // solution Array holds the order you want for your Objects
    return solution;
  }
  var newArrayOfObjects = youMakeIt(arrayOfObjects);
});
StackSlave
  • 10,198
  • 2
  • 15
  • 30
  • Thanks for the help. Throws "Uncaught TypeError: Cannot read property '0' of undefined" error here: arrayOfObjects.push({data:rz, utcday:rz.data[0].createdOn, ltrDay:moment.utc(utcday).format('DD MM YY')}); – user1452893 Oct 25 '14 at 00:39
  • No joy. Double checked everything on my side. – user1452893 Oct 25 '14 at 01:10
  • The real problem is that `arguments` is an Array Like Object that is for the `.done(function(/*really here*/){})` Anonymous function. Maybe you need to put all of the `$.getJSON()` queries in an Array. – StackSlave Oct 25 '14 at 21:13
  • Thanks for the help. All set now. – user1452893 Oct 25 '14 at 21:52
0

Unless the URLs you are making request to that serve up the JSON accept parameters to sort by date, you'll have to do it after you have all the data. Something like this should work

var date_sort_desc = function (date1, date2) {
  if (date1 > date2) return -1;
  if (date1 < date2) return 1;
  return 0;
};

var dates = [];

$.when( $.getJSON(url0), $.getJSON(url1), ... ).done( function() {

  $.each(arguments, function (index, result) {
      var data   = result[0];
      var utcday =  data[0].createdOn;

      dates.push(moment.utc(utcday).format("YY MM DD"));
  });

  dates.sort(date_sort_desc);

});
elzi
  • 4,734
  • 6
  • 32
  • 59
  • Thanks, I can see how that takes care of individual dates but I need to use lots of other JSON bits that are returned too. – user1452893 Oct 25 '14 at 00:26
  • Hope you understand what I mean. Your solution gets the dates but I have other information that I need to use returned in the JSON request. Your solution creates a new piece that is ordered properly but contains only the dates. I don't know how to include the other stuff. – user1452893 Oct 25 '14 at 13:35