I have this list:
<ul id="demoOne" class="demo">
<li><a href='http://site/Service.aspx?shId=1154'>Dhenie fotokopje aktesh nga dosja 12</a></li>
<li><a href='http://site/Service.aspx?shId=1155'>Depozitim kerkesash Ankimore/Rekurse
kunder vendimeve civile/penale 12</a></li>
<li><a href='http://site/Service.aspx?shId=1156'>Dhenie Vendimesh12</a></li>
<li><a href='http://site/Service.aspx?shId=1157'>Integrimi i Ish te Perndjekurve Polikite
12</a> </li>
<li><a href='http://site/Service.aspx?shId=1158'>Dhenie Drejtesie</a></li>
<li><a href='http://site/Service.aspx?shId=1159'>Gjykata e Rrethit Gjyqësor Lezhë ushtron
juridiksionin gjyqësor civil dhe penal në territorin e qarkut Lezhë të Republikës
së Shqipërisë.</a></li>
</ul>
I want to alphabetically sort this list, using the anchor text, not the li text. How to do that?
I'm using this script to sort them by li text
function sortUnorderedList(ul, sortDescending) {
var items = $('.demo li').get();
items.sort(function(a,b){
var keyA = $(a).text();
var keyB = $(b).text();
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
var keyA = $(a).text();
var keyB = $(b).text();
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
});
var ul = $('.demo');
$.each(items, function(i, li){
ul.append(li);
});
}
I have this list:
<ul id="demoOne" class="demo">
<li><a href='http://site/Service.aspx?shId=1154'>Dhenie fotokopje aktesh nga dosja 12</a></li>
<li><a href='http://site/Service.aspx?shId=1155'>Depozitim kerkesash Ankimore/Rekurse
kunder vendimeve civile/penale 12</a></li>
<li><a href='http://site/Service.aspx?shId=1156'>Dhenie Vendimesh12</a></li>
<li><a href='http://site/Service.aspx?shId=1157'>Integrimi i Ish te Perndjekurve Polikite
12</a> </li>
<li><a href='http://site/Service.aspx?shId=1158'>Dhenie Drejtesie</a></li>
<li><a href='http://site/Service.aspx?shId=1159'>Gjykata e Rrethit Gjyqësor Lezhë ushtron
juridiksionin gjyqësor civil dhe penal në territorin e qarkut Lezhë të Republikës
së Shqipërisë.</a></li>
</ul>
I want to alphabetically sort this list, using the anchor text, not the li text. How to do that?
I'm using this script to sort them by li text
function sortUnorderedList(ul, sortDescending) {
var items = $('.demo li').get();
items.sort(function(a,b){
var keyA = $(a).text();
var keyB = $(b).text();
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
var keyA = $(a).text();
var keyB = $(b).text();
if (keyA < keyB) return -1;
if (keyA > keyB) return 1;
return 0;
});
var ul = $('.demo');
$.each(items, function(i, li){
ul.append(li);
});
}
Share
Improve this question
edited Nov 15, 2012 at 9:49
MIlena
asked Nov 15, 2012 at 9:41
MIlenaMIlena
2571 gold badge4 silver badges13 bronze badges
3
- Do you know how to do it when using the li? If you do you should just change the identifier to $("li a") – PaperThick Commented Nov 15, 2012 at 9:45
- $("li a") - this is bad way, it takes too long to work. You better add some class to all needed anchors and then pick em with $(".anchorClass"). – Vladislav Qulin Commented Nov 15, 2012 at 9:47
- How is this HTML code generated? Can't you sort in there? – Geert Commented Nov 15, 2012 at 9:48
3 Answers
Reset to default 11try this:
$(function() {
$.fn.sortList = function() {
var mylist = $(this);
var listitems = $('li', mylist).get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : 1;
});
$.each(listitems, function(i, itm) {
mylist.append(itm);
});
}
$("ul#demoOne").sortList();
});
You can use sort
method.
$('#demoOne li').sort(function(a, b){
return $('a', a).text() > $('a', b).text()
}).appendTo('#demoOne');
http://jsfiddle.net/w3jfF/
You can use jQuery to get all the li
. Then use toArray
to get a javascript native array. And you can sort your array using the sort
method and providing a sort function corresponding to your needs.