按字母排序对象列表

时间:2013-11-16 12:38:15

标签: javascript jquery json sorting

如何在Javascript中按分隔符按字母顺序对元素列表进行排序。

我有json文件:

{"response":{"count":4,"items":[{"id":153684554,"first_name":"Adam"},{"id":153684554,"first_name":"Bob"},{"id":153684554,"first_name":"Caleb"},{"id":153684554,"first_name":"Alex"}]}}

我需要对那些对象进行排序

A.
  A....
B. 
  B....
  B....
C.
.......

在我的Jquery移动插件中:

<ul data-role="listview" data-filter="true" data-inset="true">
                <li data-role="list-divider">A</li>
                <li><a href="index.html">Adam Kinkaid</a></li>
                <li><a href="index.html">Alex Wickerham</a></li>
                <li><a href="index.html">Avery Johnson</a></li>
                <li data-role="list-divider">B</li>
                <li><a href="index.html">Bob Cabot</a></li>
                <li data-role="list-divider">C</li>
                <li><a href="index.html">Caleb Booth</a></li>
            </ul>

如何使用for运算符实现该功能?提前致谢,对不起我的英语!

4 个答案:

答案 0 :(得分:1)

更容易进行递归。

在items数组上循环,并将index的first_name与first_name index + 1进行比较。

如果index + 1不存在,那么你就在数组的末尾。

如果你交换,从index-1开始验证前一个元素。

var json = {"count":4,"items":[{"id":153684554,"first_name":"Adam"},{"id":153684554,"first_name":"Bob"},{"id":153684554,"first_name":"Caleb"},{"id":153684554,"first_name":"Alex"}]};

function sort(json, index) {
   if (json.length-1 < index+1)
       return json;
   if (json[index].first_name > json[index+1].first_name) {
        var tmp = json[index];
        json[index] = json[index+1];
        json[index+1] = tmp;
        return sort(json, index-1);
   }
   return sort(json, index+1);
}

sort(json.items, 0);

然后创建ul / li集,循环排序的json并检查first_name首字母是否与前一个字母不同,并添加li data-role属性。

if (divider != json[i].first_name[0]) {
  "<li data-role="+json[i].first_name[0]+">";
  divier = json[i].first_name[0]
}

答案 1 :(得分:1)

你可以这样:

var json = {"response":{"count":4,"items":[{"id":153684554,"first_name":"Adam"},{"id":153684554,"first_name":"Bob"},{"id":153684554,"first_name":"Caleb"},{"id":153684554,"first_name":"Alex"}]}}

var users = [];

$.each(json.response.items, function( key, user ) {
  users.push(user.first_name);
});

users.sort();

var existent_letters = [];
$.each(users, function( key, user ) {
  var current_letter = user.charAt(0);
  existent_letters.push(current_letter);
});

$.unique(existent_letters);

var return_html = "";
$.each(existent_letters, function( keyLetter, letter ) {
  return_html += '<li class="separator">'+letter+'</li>';
  $.each(users, function( keyUser, user ) {
    if(user.indexOf(letter) == 0) {
        return_html += '<li>'+user+'</li>';
    }
  });
});
$('#listview').html(return_html);

我为你准备了JSFiddle to illustrate this code

答案 2 :(得分:0)

检查此fiddle

document.addEventListener('DOMContentLoaded',function(){
    var a = {"response": {"count": 4, "items": [
        {"id": 153684554, "first_name": "Adam"},
        {"id": 153684554, "first_name": "Bob"},
        {"id": 153684554, "first_name": "Caleb"},
        {"id": 153684554, "first_name": "Alex"}
    ]}}

    var b = a.response.items.sort(function (a, b) {
        return a.first_name>b.first_name;
    })
    var buffer="";
    for(i in b){
        buffer+="<li data-role='list-divider'>"+b[i].first_name+"</li>";
    }
    document.getElementsByTagName('ul')[0].innerHTML=buffer;
},false);

答案 3 :(得分:0)

试试这个:

var data = {"response":{"count":4,"items":[{"id":153684554,"first_name":"Adam"},{"id":153684554,"first_name":"Bob"},{"id":153684554,"first_name":"Caleb"},{"id":153684554,"first_name":"Alex"}]}};
data.response.items.sort(compare);
show(data);
function compare(a,b) {
        if (a.first_name < b.first_name)
            return -1;
        if (a.first_name > b.first_name)
            return 1;
        return 0;
    }
function show(data) {
    var len = data.response.items.length;
    for (var i = 0; i < len; i++) {
        var t = data.response.items[i];
        var role = t.first_name.substr(0, 1).toUpperCase();
        if ($("body").find("li[data-role='list-divider']:contains(" + role + ")").length < 1){            
            $("body").append("<li data-role='list-divider'>" + role + "</li>");
        }
        $("body").append("<li>" + t.first_name + "</li>");
    }
}

Fiddle here.