sort jquery result alphabetically

浪子不回头ぞ 提交于 2019-12-23 05:41:16

问题


I am trying to make a dynamic list of my blog posts. I need the list to be displayed alphabetically. The current code is working okay, but gave me a chronological list. How can I arrange my list alphabetically. Current code is given below. It is for blogger blog and I used kimonolabs to make the API used in this code. The feed is in jason. (In blog page area I first created a blank html list and then used below code to insert data. Html is also given.) What should I do to make the result alphabetical.

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>

回答1:


As response.results.collection1 is array, and you want it to order in alphabetical order, you need to sort by each item's 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>



回答2:


http://jsfiddle.net/03f1ehsf/

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


来源:https://stackoverflow.com/questions/33146801/sort-jquery-result-alphabetically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!