I need to sort some elements depend on it attribute. For an example:
<div id="sort">
<div n="1"></div>
<div n="2"></div>
<div n="3"></div>
<div n="4"></div>
</div>
And array_num
{3, 2, 1, 4}
pseudo code:
$('#sort').sort(array_num, 'n');
results will be:
<div id="sort">
<div n="3"></div>
<div n="2"></div>
<div n="1"></div>
<div n="4"></div>
</div>
                var order = [3, 2, 4, 1];
var el = $('#sort');
var map = {};
$('#sort div').each(function() { 
    var el = $(this);
    map[el.attr('n')] = el;
});
for (var i = 0, l = order.length; i < l; i ++) {
    if (map[order[i]]) {
        el.append(map[order[i]]);
    }
}
Full code here 
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With