Sorting XML created by ajax call
I have an ajax call that pulls data from a MS SQL databse using an API. I'm trying to sort based on the CNT_SEQ_ID element of the XML, then create a list of three items to put inside a div in the HTML. I have code that does everything but the sort :
- $.ajax({
type: "POST",
async: false,
data: strParams,
url: "/AJAXUtilities.aspx",
success: function(msg){
msg = $.createXMLDocument(msg);
//$("#newscatlist").html(msg); //show results
var li_count = 0;
$(msg).find('CONTENT_SEARCH').each(function(){
if ($(this).find("CNT_AVAILABLE").text() == "T") {
var objid = $(this).find("CNT_CONTENT_ID").text();
var title = $(this).find("CNT_TITLE").text();
if (title.length >= 40){
var trimtitle = title.substring(0,39);
list = list.concat("<li><a href=\"/MyOptions.aspx?id=" + objid + "\">" + trimtitle +"...</a></li><hr>");
}else{
list = list.concat("<li><a href=\"/MyOptions.aspx?id=" + objid + "\">" + title +"</a></li><hr>");
}
li_count = li_count+1;
if (li_count == 3) {
return false;
}
}
});
}
});
list = list.concat("</ul>");
list = list.concat("<br /><a href=\"\">View previous clinical updates>></a>");
$("#newscatlist").append(list);
return list;
Here's my modified code attempting to do the sort:
- $.ajax({
type: "POST",
async: false,
data: strParams,
url: "/AJAXUtilities.aspx",
success: function(msg){
msg = $.createXMLDocument(msg);
var li_count = 0;
var results = [];
$(msg).find('CONTENT_SEARCH').each(function(){
if ($(this).find("CNT_AVAILABLE").text() == "T") {
var seqtext = $(this).find("CNT_SEQ_ID").text();
results.push({
seq: seqtext,
objid: $(this).find("CNT_CONTENT_ID").text(),
title: $(this).find("CNT_TITLE").text()
});
results.sort(function (a, b) {return a.seqtext > b.seqtext;});
var html = [];
html.unshift("<ul style=\"list-style: none; text-indent: -1.2em;\" class=\"newsupdates-list\" id=\"newsupdates-" + strCategoryID + "\">");
newscatlist.innerHTML=html;
if (title.length >= 40){
var trimtitle = title.substring(0,39);
html.push("<li><a href=\"/MyOptions.aspx?id=" + this.objid + "\">" + this.trimtitle +"...</a></li><hr>");
}else{
html.push("<li><a href=\"/MyOptions.aspx?id=" + this.objid + "\">" + this.title +"</a></li><hr>");
}
li_count = li_count+1;
if (li_count == 3) {
return false;
}
}
});
html.push("<br /><a href=\"\">View previous clinical updates>></a>");
$("#newscatlist").append(html.join(''));
}
});
What am I doing wrong?