【问题标题】:Sort a JSON alphabetically using JQuery and print to a table使用 JQuery 按字母顺序对 JSON 进行排序并打印到表格
【发布时间】:2017-09-29 15:13:31
【问题描述】:

所以我尝试获取一个 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"
}]

【问题讨论】:

    标签: javascript jquery json object


    【解决方案1】:

    如果你想按名称对对象数组进行排序,试试这个:

    var jsonArray; //your json array
    jsonArray.sort(function(a,b){
       returb a.name > b.name;
    });
    

    array sorting

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 2021-11-20
      相关资源
      最近更新 更多