因此,我尝试使用json并按字母顺序对其进行排序,然后创建表并将信息动态放入各自的表中。我似乎无法让它们按字母顺序排序。下面是我的JQuery:
<script>
function populateDept(){
$.ajax({
type: 'GET',
url: 'https://dl.dropboxusercontent.com/s/sslmd5y53uusgfx/depts.json',
data: { get_param: 'value' },
dataType: 'json',
success: function (data) {
$.each(data, function(index, element) {
data.sort(function(a,b){
return a[1] > b[1] ? 1: -1;
});
var classAcr ="." + element.acronym;
$('#content').prepend($('<table class="aui aui-sortable">').addClass(element.acronym + ""));
$(classAcr).append($('<thead>').css("text-align", "left").append($('<tr>').append($('<th>', {
text: element.name
})).append($('<th>', {
text: element.acronym
}))));
for (var i = 0; i < element.departments.length; i++) {
$(classAcr).append($('<tr>').append($('<td>', {
text: element.departments[i].name
})).append($('<td>', {
text: element.departments[i].acronym
})));
}
});
$("th").css("width","50%");
$("th").css("width","50%");
$("table").append("<br>");
$("th").css("font-weight", "bold");
$("th").css("font-size", "35px");
}
});
}
populateDept();
</script>
我正在使用的json看起来像这样,但列出了更多的部门。我试图按“名称”排序他们:“学术事务”等。
[{
"name": "Academic Affairs",
"acronym": "ACAD",
"departments": [{
"name": "International Education and Global Programs",
"acronym": "IEGP"
},
{
"name": "Graduate Studies",
"acronym": "GRAD"
},
{
"name": "RIT Honors Program",
"acronym": "Honors"
},
{
"name": "Institute Advising",
"acronym": "IADV"
},
{
"name": "Innovative Learning Institute",
"acronym": "ILI"
},
{
"name": "K12 Programs",
"acronym": "K12"
},
{
"name": "Office of the Registrar",
"acronym": "RGR"
},
{
"name": "RIT Online",
"acronym": "RITO"
},
{
"name": "Study Abroad Program",
"acronym": "SAP"
},
{
"name": "Student Learning Outcomes Assessment",
"acronym": "SLOA"
},
{
"name": "School of Individualized Study",
"acronym": "SOIS"
},
{
"name": "Teaching and Learning Studio",
"acronym": "TLS"
},
{
"name": "University Studies Program",
"acronym": "USP"
},
{
"name": "University Writing Program",
"acronym": "UWP"
}]
答案 0 :(得分:0)
如果要按名称对对象数组进行排序,请尝试以下方法:
var jsonArray; //your json array
jsonArray.sort(function(a,b){
returb a.name > b.name;
});