Hope this helps. Updated the id to account for alphabetical ordering 1 and 11.
<div id="main">
<div id="dv_07">7...</div>
<div id="dv_01">1...</div>
<div id="dv_08">8...</div>
<div id="dv_04">4...</div>
<div id="dv_11">11...</div>
<div id="dv_02">2...</div>
</div>?
jQuery option:
var mylist = $('#main');
var listitems = mylist.children('div').get();
listitems.sort(function(a, b) {
var compA = $(a).attr('id').toUpperCase();
var compB = $(b).attr('id').toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) {
mylist.append(itm);
});?
Javascript option:
var mylist = document.getElementById('main');
var divs = mylist.getElementsByTagName('div');
var listitems = [];
for (i = 0; i < divs.length; i++) {
listitems.push(divs.item(i));
}
listitems.sort(function(a, b) {
var compA = a.getAttribute('id').toUpperCase();
var compB = b.getAttribute('id').toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
});
for (i = 0; i < listitems.length; i++) {
mylist.appendChild(listitems[i]);
}?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…