Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Table sorter
$.fn.tableSort = function (col, order){
function toNumber(number) {
return parseFloat(number.replace(/\./g,"").replace(/,/g,"."))
}
return this.each(function(){
var rows = [].slice.call(this.rows, 0).sort(function(a,b){
if (order === 'desc') b = [a,a=b][0]; // troca-troca
var textA = $(a.cells[col]).text()
, textB = $(b.cells[col]).text()
if (/^[-0-9.,]*$/.test(textA)) {
textA = toNumber(textA)
textB = toNumber(textB)
console.log(textA)
}
return textA > textB
? 1
: textA < textB
? -1
: 0;
});
$(this).html( rows );
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.