Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

html - sorting an ul list according to a li element with jquery?

I'm creating a horizontal list with < ul > element but what I want to do is to put each < ul > element in alphabetical order according to a < li > element. For example

<ul class="list">
    <li>Name</li>
    <li>Surname</li>
    <li>Unit</li>
    <li>City</li>
</ul>
<ul class="list">
    <li>John</li>
    <li>Boe</li>
    <li>B.A.</li>
    <li>NY</li>
</ul>
<ul class="list">
    <li>Jane</li>
    <li>Doe</li>
    <li>M.A.</li>
    <li>CA</li>
</ul>

and put them in order according to Surname field. Is there any way to do this with jquery?

Thanks in advance

//EDIT//

The code I'm using is something like this

$.ajax({
    type: 'GET',
    url: 'list.php',
    data: 'id='+id,
    dataType: 'xml',
    success: function(data) {
        var xml;
        if (typeof data == "string") {
            xml = new ActiveXObject("Microsoft.XMLDOM");
            xml.async = false;
            xml.loadXML(data);
        } else {
            xml = data;
        }

        $(xml).find('item').each(function(){
            var id = $(this).find("id").text();
            var name = $(this).find("name").text();
            var surname = $(this).find("surname").text();
            var units = $(this).find("units").text();
            var city = $(this).find("city").text();
            $("#listContainer").append('<ul class="list" id="'+id+'"><li style="width:30px;">'+name+'</li><li style="width:100px;">'+surname+'</li><li style="width:100px;">'+units+'</li><li style="width:100px;">'+city+'</li></ul>');
        }); //close each(   
    }
}); 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

WORKING DEMO

$('.grid ul:gt(0)').each(function() {
    var txt1 = $(this).children('li:eq(1)').text();
    $(this).data('name', txt1);
});

var items = $('.grid ul');
items.sort(function(a, b) {
    var chA = $(a).data('name');
    var chB = $(b).data('name');
    if (chA < chB) return -1;
    if (chA > chB) return 1;
    return 0;
});
var grid = $('.grid');
$(grid).append(items);

and the HTML

<div class="grid">
    <ul class="list">
        <li>Name</li>
        <li>Surname</li>
        <li>Unit</li>
        <li>City</li>
    </ul>
    <ul class="list">
        <li>John</li>
        <li>Boe</li>
        <li>B.A.</li>
        <li>NY</li>
    </ul>
    <ul class="list">
        <li>Jane</li>
        <li>Doe</li>
        <li>M.A.</li>
        <li>CA</li>
    </ul>
    <ul class="list">
        <li>Lin</li>
        <li>Zyan</li>
        <li>M.A.</li>
        <li>OR</li>
    </ul>
    <ul class="list">
        <li>Matt</li>
        <li>Albright</li>
        <li>M.A.</li>
        <li>CA</li>
    </ul>
</div>

Will result in:

enter image description here

How it works:
We look for the text inside the desired li :eq()
and we set this text as a jQuery .data() for each UL element.
Than we just sort the ul by the retrieved .data() text - alphabetically!

Happy coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...