按字母顺序排序jquery结果

时间:2015-10-15 11:03:28

标签: javascript jquery list blogger kimono

我正在尝试制作我的博文的动态列表。我需要按字母顺序显示列表。目前的代码工作正常,但给了我一个按时间顺序排列的列表。如何按字母顺序排列列表。目前的代码如下。这是博客博客,我使用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
      }
  }
 
  })
&#13;
<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

http://jsfiddle.net/03f1ehsf/

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