-
-
Save grifo/1719583 to your computer and use it in GitHub Desktop.
Table sorter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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