首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JQuery按字母顺序对JSON进行排序并打印到表中

使用JQuery按字母顺序对JSON进行排序并打印到表中
EN

Stack Overflow用户
提问于 2017-09-29 23:13:31
回答 1查看 1.4K关注 0票数 0

因此,我尝试获取一个json并按字母顺序对其进行排序,然后创建表并将信息动态地放入各自的表中。我似乎不能让他们按字母顺序排序。下面是我的JQuery:

代码语言:javascript
复制
<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看起来像这样,但是列出了更多的部门。我试着按“名字”:“学术事务”等来排序。

代码语言:javascript
复制
[{
"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"
}]
EN

回答 1

Stack Overflow用户

发布于 2017-09-29 23:36:03

如果您想按名称对对象数组进行排序,请尝试以下方法:

代码语言:javascript
复制
var jsonArray; //your json array
jsonArray.sort(function(a,b){
   returb a.name > b.name;
});

array sorting

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46491895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档