Skip to content

Instantly share code, notes, and snippets.

@lucawen
Created February 21, 2017 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lucawen/ff5d9527e489aa40a7fb6b1693ebe476 to your computer and use it in GitHub Desktop.
Save lucawen/ff5d9527e489aa40a7fb6b1693ebe476 to your computer and use it in GitHub Desktop.
function FilterByAttribute(mainElementName, itemElementName, attributeName, addSpace){
addSpace = addSpace || false;
var divList = $(itemElementName);
divList.sort(function (a, b) {
var attributeA = $(a).data(attributeName);
var attributeB = $(b).data(attributeName);
if(IsStringNumber(attributeA) || IsStringNumber(attributeB)){
return $(a).data(attributeName) > $(b).data(attributeName);
} else {
return String.prototype.localeCompare.call(attributeA.toLowerCase(), attributeB.toLowerCase());
}
});
$(mainElementName).html(divList);
if(addSpace){
$(mainElementName+" hr.hr-agora").remove();
$(mainElementName+" "+itemElementName+":not(:first)").prepend("<hr class='hr-agora'>");
}
}
function IsStringNumber(value) {
return Math.floor(value) == value && $.isNumeric(value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment