Following this post jQuery table sort (github link: https://github.com/padolsey/jQuery-Plugins/blob/master/sortElements/jquery.sortElements.js), I am successfully sort columns, however it does not work in the case of rowspan: For example, case like this
Grape 3,096,671M
1,642,721M
Apple 2,602,750M
3,122,020M
When I click on the second column, it try to sort
Apple 2,602,750M
1,642,721M
Grape 3,096,671M
3,122,020M
which as you can see is not correct, please any jQuery guru help me fix this problem. Here is my code
var inverse = false;
function sortColumn(index){
index = index + 1;
var table = jQuery('#resultsTable');
table.find('td').filter(function(){
return jQuery(this).index() == index;
}).sortElements(function(a, b){
a = convertToNum($(a).text());
b = convertToNum($(b).text());
return (
isNaN(a) || isNaN(b) ?
a > b : +a > +b
) ?
inverse ? -1 : 1 :
inverse ? 1 : -1;
},function(){
return this.parentNode;
});
inverse = !inverse;
}
function convertToNum(str){
if(isNaN(str)){
var holder = "";
for(i=0; i<str.length; i++){
if(!isNaN(str.charAt(i))){
holder += str.charAt(i);
}
}
return holder;
}else{
return str;
}
}
Question:
1.How do I sort this with rowspan. THE NUMBER OF ROWSPAN IS NOT ALWAYS THE SAME. The above example both Grape and Apple have rowspan of 2, but this is not always the case.
2.Can any explain this syntax:
return (
isNaN(a) || isNaN(b) ?
a > b : +a > +b
) ?
inverse ? -1 : 1 :
inverse ? 1 : -1;
So I can see that if either a or b is not a number, then do string comparison otherwise do number comparison, but I dont understand the
inverse ? -1 : 1 :
inverse ? 1 : -1;