I stumbled across this trying to fix what I was after.
I took @shrikant-sharat's method and added a little to it as the attribute I needed to sort on was actually on a child element. Thought I'd add here in case it helps anyone (and for future me!)
$.fn.asort = function (order, attrName, filter) {
console.log(this.length, order.length, order);
for(var i = 0, len = order.length; i < len; ++i) {
if(typeof(filter) === 'function') {
filter(this.children(), attrName, order[i]).appendTo(this);
} else {
this.children('[' + attrName + '=' + order[i] + ']').appendTo(this);
}
}
return this.children();
}
It allows you to pass a filter function to match the element you're after. It's not the most efficient I suppose, but it works for me, e.g.:
$('.my-list').asort(mapViewOrder, 'data-nid', function(items, attrName, val) {
return items.filter(function(index, i) {
return ($(i).find('[' + attrName + '="' + val + '"]').length);
});
});
logicbehind that order or do you need it just exactly like that?nis not a validhtml tagattribute. It will cause you some undesired behavior on some browser.