按字母顺序排序 jquery 结果

sort jquery result alphabetically

我正在尝试制作我的博文的动态列表。我需要按字母顺序显示列表。当前代码工作正常,但给了我一个按时间顺序排列的列表。我怎样才能按字母顺序排列我的列表。当前代码如下。它用于博主博客,我使用 kimonolabs 制作了此代码中使用的 API。提要在杰森。 (在博客页面区域,我首先创建了一个空白 html 列表,然后使用下面的代码插入数据。Html 也给出了。)我应该怎么做才能使结果按字母顺序排列。

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>

由于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>

http://jsfiddle.net/03f1ehsf/

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