1

I have a function pulling data from a MySQL database and outputting JSON. Here is an example of the result:

{"success":"true","message":"Data loaded","data":{"11":{"id":"11","timestamp2":1349091206,"timestamp":" 1. oktober kl. 13:33","date_from":"25. oktober kl. 8:45","date_to":"25. oktober kl. 8:45","initials":"rt","trafikselskab":"Sydtrafik","alarm_comment":"sr6yer6y","transport_comment":"et6et7","vogn_comment":"uet7uet7","events_comment":"rt7ujrt7ury7u","system_comment":"urt7j","other_comment":"ry8kykytik"},"10":{"id":"10","timestamp2":1349091195,"timestamp":" 1. oktober kl. 13:33","date_from":" 1. oktober kl. 1:40","date_to":" 1. oktober kl. 1:40","initials":"rt","trafikselskab":"Sydtrafik","alarm_comment":"sr6yer6y","transport_comment":"et6et7","vogn_comment":"uet7uet7","events_comment":"rt7ujrt7ury7u","system_comment":"urt7j","other_comment":"ry8kykytik"},"9":{"id":"9","timestamp2":1349089753,"timestamp":" 1. oktober kl. 13:09","date_from":"25. oktober kl. 8:15","date_to":"25. oktober kl. 14:15","initials":"PSH","trafikselskab":"Midttrafik","alarm_comment":"ftyhdrtyh","transport_comment":"dtyjtr7j","vogn_comment":"rtyujkytik","events_comment":"yuilyiol","system_comment":"tuikul","other_comment":"yuolyu9lio"},"8":{"id":"8","timestamp2":1348230434,"timestamp":"21. september kl. 14:27","date_from":"27. september kl. 8:30","date_to":"21. september kl. 8:30","initials":"PSH","trafikselskab":"Movia","alarm_comment":"srtyh","transport_comment":"dyh","vogn_comment":"dtyjhdtyj","events_comment":"teyhtryj","system_comment":"dtyhteyj","other_comment":"trydjrtyuj"}}}

I have a JS function that takes the data from the JSON and puts it in HTML tags:

function overlevering() {
$.getJSON('/test/ajax2.php?type=test', function(data) {
    if(data.data != '') {
        $.each(data.data, function(fravaer, type) {
                $('#overlevering').append('<article id="'+ type.timestamp2 +'"><h2>Modtaget '+ type.timestamp +'</h2><p class="from">Periode: '+ type.date_from +' - '+ type.date_to +'</p><p class="from">Fra '+ type.initials +', '+ type.trafikselskab +'</p><h3>Alarmer, der kræver særlig opmærksomhed</h3><p>'+type.alarm_comment+'</p><h3>Information vedr. teletaxer, flexture og handicapture</h3><p>'+type.transport_comment+'</p><h3>Information vedr. vogne, vognmænd og centraler</h3><p>'+type.vogn_comment+'</p><h3>Information vedr. systemer</h3><p>'+type.system_comment+'</p><h3>Information vedr. begivenheder, der kan påvirke driften</h3><p>'+type.events_comment+'</p><h3>Øvrige</h3><p>'+type.other_comment+'</p></article>');

        });
    }
});
}

When visiting the results page in Safari, the results are sorted as they are in the JSON (by date). However, in Chrome it seems they aren't sorted after any rules, just thrown in there.

I guess it isn't possible to sort in the JSON, so how do I get around this, and always show the newest one on top?

pshoeg
  • 379
  • 2
  • 9
  • 20
  • Where is your code you are using to sort? Or are you purely relying on default orders provided to you from the server? – Nope Oct 01 '12 at 12:32
  • Yeah, the sorting is done in the SQL query. – pshoeg Oct 01 '12 at 12:34
  • Odd. `Append()` should always add the current objects to the end of the specified elements. Are the json elements coming back inthe correct order? If it's SQL sorted, I'm sure they are... – Dutchie432 Oct 01 '12 at 12:34
  • 1
    Yes, they are. However, I think I remember reading something about Chrome ordering JSON output differently than other browsers. – pshoeg Oct 01 '12 at 12:36
  • Weird thing is, when I check the JSON output in Chrome, it's in the correct order, but when it's added to the page using append() it's in the wrong order. – pshoeg Oct 01 '12 at 13:16

2 Answers2

0

It is actually possible to sort a JSON array. Basically it's just a javascript object once you get it.

Here is an example : Sorting JSON by values

Community
  • 1
  • 1
PofMagicfingers
  • 215
  • 2
  • 14
0

Ah, I figured it out. I'm sorting the data after getJSON using JS's sort() method:

    var articles = $("#overlevering article");

        articles.sort(function(a,b) {
        if (a.id < b.id) return 1;
        else if (a.id > b.id) return -1;
        else return 0
    })

    $("#overlevering").empty().append( articles );
pshoeg
  • 379
  • 2
  • 9
  • 20