I have an HTML structure as follows:
-
A more generic function to sort elements using jQuery:
$.fn.sortChildren = function (sortingFunction: any) {
return this.each(function () {
const children = $(this).children().get();
children.sort(sortingFunction);
$(this).append(children);
});
};
Usage:
$(".clist").sortChildren((a, b) => a.dataset.sid > b.dataset.sid ? 1 : -1);