2015-10-15 224 views
0

我正在嘗試製作我的博客文章的動態列表。我需要列表按字母順序顯示。目前的代碼工作正常,但給了我一個時間順序列表。我如何按字母順序排列我的列表。目前的代碼如下。這是博客博客,我用kimonolabs來製作這個代碼中使用的API。飼料是傑森。 (在博客頁面區域,我首先創建了一個空白html列表,然後使用下面的代碼插入數據,同時給出了Html。)我應該怎麼做才能使結果按字母順序排列。按字母順序排序jquery結果

jQuery.ajax({ 
 
    "url":"https://www.kimonolabs.com/api/djwmp1p8?apikey=P1DP0fILX0ou5GnXR6DRbbRmkFuQNC0G", 
 
    "crossDomain":true, 
 
    "dataType":"jsonp", 
 
    //Make a call to the Kimono API following the "url" 
 
    
 
    'success': function(response){ 
 
    // If the call request was successful and the data was retrieved, this function will create a list displaying the data 
 
     
 
    jQuery(".panel-heading").html(response.name); 
 
    //Puts the API name into the panel heading 
 
     
 
    var collection = response.results.collection1; 
 
    for (var i = 0; i < collection.length; i++){ 
 
    // Traverses through every element in the entire collection 
 
     
 
     jQuery(".list-group").append('<li class="list-group-item">' +'<a href='+collection[i].property1.href +'>'+ collection[i].property1.text + '</a>' +'</li>'); 
 
     // adds the text and the links from the first property into the list 
 
     } 
 
    } 
 
    
 
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
<div class="container padding"> 
 
    <div class="panel panel-info"> 
 
    <div class="panel-heading"></div> 
 
    <ol class="list-group"> 
 
    </ol> 
 
    </div> 
 
</div>

+0

有你看着http://stackoverflow.com/questions/881510/sorting-json-by-values –

回答

3

由於response.results.collection1array,並且希望它按字母順序排序,你需要理清每個項目的property1.text

collection.sort(function(item1, item2) { 
    return item1.property1.text > item2.property1.text ? 1 : -1; 
}); 

jQuery.ajax({ 
 
    "url":"https://www.kimonolabs.com/api/djwmp1p8?apikey=P1DP0fILX0ou5GnXR6DRbbRmkFuQNC0G", 
 
    "crossDomain":true, 
 
    "dataType":"jsonp", 
 
    //Make a call to the Kimono API following the "url" 
 
    
 
    'success': function(response){ 
 
    // If the call request was successful and the data was retrieved, this function will create a list displaying the data 
 
     
 
    jQuery(".panel-heading").html(response.name); 
 
    //Puts the API name into the panel heading 
 
     
 
    var collection = response.results.collection1; 
 
    // VVVV Sort it by item.property1.text before print out. 
 
    collection.sort(function(item1, item2) { 
 
     // If item1.property1.text's alphabetical order is larger than item2's return 1, otherwise return 0. 
 
     return item1.property1.text > item2.property1.text ? 1 : -1; 
 
     //return item1.property1.text.localeCompare(item2.property1.text) > 0 ? 1 : -1; 
 
    }); 
 
    for (var i = 0; i < collection.length; i++){ 
 
    // Traverses through every element in the entire collection 
 
     
 
     jQuery(".list-group").append('<li class="list-group-item">' +'<a href='+collection[i].property1.href +'>'+ collection[i].property1.text + '</a>' +'</li>'); 
 
     // adds the text and the links from the first property into the list 
 
     } 
 
    } 
 
    
 
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
 
<div class="container padding"> 
 
    <div class="panel panel-info"> 
 
    <div class="panel-heading"></div> 
 
    <ol class="list-group"> 
 
    </ol> 
 
    </div> 
 
</div>

+0

我的情況。我投這個最好的答案。很好的幫助。謝謝。 – Nesi

+0

不客氣:)高興地幫忙。 – fuyushimoya

+0

@Nesi,請將它標記爲已接受的答案;) – nicolallias

0

http://jsfiddle.net/03f1ehsf/

collection.sort(function(a,b){ return b.property1.text>a.property1.text?0:1}); 
+0

這一個不會使它按字母順序排列。現在名單混淆了。由fuyushimoya提供的解決方案正在工作。 – Nesi