Skip to content

Instantly share code, notes, and snippets.

@grifo
Forked from ricardobeat/jquery.sort.coffee
Created February 1, 2012 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grifo/1719583 to your computer and use it in GitHub Desktop.
Save grifo/1719583 to your computer and use it in GitHub Desktop.
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