按字母顺序排序jquery结果



我正在尝试为我的博客文章创建一个动态列表。我需要按字母顺序显示列表。目前的代码工作正常,但给了我一个时间顺序列表。我该如何按字母顺序排列我的列表?当前代码如下所示。这是为博主的博客和我使用kimonolabs使API在这段代码中使用。提要在jason中。(在博客页面区域,我首先创建了一个空白的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}); 

最新更新