问题
I'm using jQuery UI to create several, interconnected sortable lists
the html:
<div class="ulContainer">
<div class="liHdr">Unassigned</div>
<div class="ulWraper">
<ul class="connectedSortableUl un ui-sortable">
<li class="ui-state-default" style="">Frank Smith
<input type="hidden" class="rowName" value="frank.smith">
<input type="hidden" class="rowEmail" value="frank.smith@email.com">
<input type="hidden" class="rowId" value="8VNe0ZT1v0">
<input type="hidden" class="rowTeam" value="">
<div class="panel-options" style="float:right;"><a href="#" class="sm"><i class="entypo-pencil"></i></a></div>
</li>
</ul>
</div>
</div>
and so on....several more of the same
the jQuery:
// create the sortable ui
$(".connectedSortableUl").sortable({
connectWith: ".connectedSortableUl",
placeholder: "ui-state-highlight",
create: function(event, ui) {
sort();
}
});
// custom sort function to sort our sortable lists
function sort() {
var sortableLists = $('.connectedSortableUl');
$(sortableLists).each(function(index, element) {
var listitems = $('li', element);
listitems.sort(function(a, b) {
var seeA = $(a).text().toUpperCase(); //just to see what's going on
var seeB = $(b).text().toUpperCase(); //just to see what's going on
return ($(a).text().toUpperCase() > $(b).text().toUpperCase())
});
$(element).append(listitems);
});
}
- I expect this function to sort each list alphabetically with A at the top and Z at the bottom
- With short lists, 13 or fewer items, this function works as expected.
- However, if a list has 14 or more items, something breaks down and the list is now longer sorted as expected.
Why does the logic break down with more than 13 list items, and what can I do to fix it or otherwise achieve the desired results?
jsFiddle example
回答1:
Comparing two strings with >
returns a boolean, but sort
expects a number. Compare with localeCompare
:
listitems.sort(function (a, b) {
var ta = $(a).text().toUpperCase();
var tb = $(b).text().toUpperCase();
return ta.localeCompare(tb);
});
来源:https://stackoverflow.com/questions/28246809/alphabetically-sort-elements-in-sortable-list-with-over-13-items