按字母顺序排序jquery结果 [英] sort jquery result alphabetically

查看:144
本文介绍了按字母顺序排序jquery结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态列出我的博客文章.我需要按字母顺序显示列表.当前代码可以正常工作,但是给了我一个按时间顺序排列的清单.如何按字母顺序排列我的列表.当前代码如下.它是针对博客博客的,我使用kimonolabs制作了此代码中使用的API.提要是jason. (在博客页面区域中,我首先创建了一个空白的html列表,然后使用下面的代码插入数据.还提供了HTML.)我应该怎么做才能使结果按字母顺序排列.

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>

推荐答案

由于response.results.collection1array,并且要按字母顺序对其进行排序,因此需要按每个项目的property1.text进行排序:

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>

这篇关于按字母顺序排序jquery结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆