0

Update: I was able to find a solution... The sort code below actually works.

I'm using jQuery (ajax) to parse and XML file, adding the data to an array, sorting it, and then displaying it. Everything works great in Firefox, but it behaves erratically in Chrome (i.e. It only partially sorts)

Here is the page: http://teachwritework.com/part-time-teaching-jobs.php

I've been at this for over 3 hours and I'm stuck. If anyone can point out the error in my ways, I would greatly appreciate it. I'm a rookie with JS / Ajax, so be gentle. My code is as follows:

$(document).ready(function () {
    var dates = [];
    $.ajax({
        type: "GET",
        url: "jobs-for-teachers.xml",
        dataType: "xml",
        success: xmlParser
    });

}); /* End get-xml */

function xmlParser(xml) {
var dates = [];
$(xml).find("job").each(function () {
    dates.push({
        jobtitle:   $(this).find("title").text(),
        jobsource:  $(this).find("source").text(),
        posted:     $(this).find("posted").text(),
        locale:     $(this).find("location").text(),
        employer:   $(this).find("employer").text(),
        comp:       $(this).find("comp").text(),
        schedule:   $(this).find("schedule").text(),
        reqs:       $(this).find("reqs").text(),
        desc:       $(this).find("desc").text(),
        contact:    $(this).find("contact").text()
    });
});  /* End parsing xml */

/* Updated code to sort in reverse chronological order */
    dates.sort(function(a,b){
        if(a.posted<b.posted) return 1;
        if(a.posted>b.posted) return -1;
        return 0;
    });


var html=[];
$.each(dates, function() {
    html.push(
        '<h2>' + this.jobtitle 
            + '</h2><br><h5>Posted: </h5>' + this.posted                
            + '<br><br><h5>Location: </h5><div class="tfield">' + this.locale
            + '<br></div><h5>Employer: </h5><div class="tfield">' + this.employer
            + '<br></div><h5>Compensation: </h5><div class="tfield">' + this.comp
            + '<br></div><h5>Schedule: </h5><div class="tfield">' + this.schedule
            + '<br></div><h5>Description: </h5><div class="tfield">' + this.desc
            + '<br><br></div><h5>Requirements: </h5><div class="tfield">' + this.reqs
            + '<br></div><h5>Contact: </h5><div class="tfield">' + this.contact
            + '<br><br></div><h5>Source: </h5><div class="tfield">' + this.jobsource 
            + '</div><br class="clr"><br><br><hr><br><br>'
    );
});
$('.listings').append(html.join(''));

} /* End xml-parser */

ojbravo
  • 45
  • 1
  • 7
  • sort function should return a value equal to 0, less than 0 or greater than 0, see question http://stackoverflow.com/questions/1969145/sorting-javascript-array-with-chrome – Sangeet Agarwal May 17 '13 at 15:27
  • @user2188275 - This was a good point in the right direction and eventually lead me to the solution. Thanks! – ojbravo May 18 '13 at 22:19

1 Answers1

0

See this question: Sorting an array of JavaScript objects

So if you add this:

var sort_by = function(field, reverse, primer){

   var key = function (x) {return primer ? primer(x[field]) : x[field]};

   return function (a,b) {
       var A = key(a), B = key(b);
       return ((A < B) ? -1 :
               (A > B) ? +1 : 0)) * [-1,1][+!!reverse];                  
   }
}

you can then do the sort like so:

// Date sort
dates.sort(sort_by('posted', false, function(a){return new Date(a)}));
Community
  • 1
  • 1
mccannf
  • 16,619
  • 3
  • 51
  • 63
  • Thank you so much for the point in the right direction! I wasn't able to get your code to work because I'm such a rookie, but it did lead me to the right answer. – ojbravo May 18 '13 at 22:14