Skip to content

Instantly share code, notes, and snippets.

@alfchee
Created October 26, 2016 22:30
Show Gist options
  • Save alfchee/0ca848c96c46899b7dd127f2d4b89f0a to your computer and use it in GitHub Desktop.
Save alfchee/0ca848c96c46899b7dd127f2d4b89f0a to your computer and use it in GitHub Desktop.
Use jQuery to sort <li> elements that posses a data-index property
// selecting the ul and children
var $categories = this.$el.find('ul.sortable'),
$categoriesli = $categories.children('li');
// sorting the list
$categoriesli.sort(function(a,b){
var an = $(a).find('div.sort-arrows').data('index'),
bn = $(b).find('div.sort-arrows').data('index');
if(an > bn) {
return 1;
}
if(an < bn) {
return -1;
}
return 0;
});
// re-attaching the ordered list
$categoriesli.detach().appendTo($categories);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment