Numeric sort with "NA" in some cells
Numeric sort with "NA" in some cells
bussemac
Posts: 1Questions: 0Answers: 0
I have a site with a lot of biological data on it, and sometimes the information just isn't available. By client request, where data is missing/unavailable we display "NA." I used sType to force a numeric sort on those columns, but it seems to only sort up to a certain point (some number of rows), then gets stuck.
See http://rrndb.mmg.msu.edu/search.php (click "Browse the entire database" and scroll down to the datatable when it finishes loading). Try sorting by 16S. Chrome seems to just ignore the N/A completely and let it fall where it may, Firefox gets hung up and must quit the first time it hits an NA or something.
Do I need a custom sort function for this (as you can probably tell from the source code, I was using sortable, which did not have this problem, but I wanted to add paging), or is there some way to say "sort numeric, treat all strings as positive/negative infinity"?
See http://rrndb.mmg.msu.edu/search.php (click "Browse the entire database" and scroll down to the datatable when it finishes loading). Try sorting by 16S. Chrome seems to just ignore the N/A completely and let it fall where it may, Firefox gets hung up and must quit the first time it hits an NA or something.
Do I need a custom sort function for this (as you can probably tell from the source code, I was using sortable, which did not have this problem, but I wanted to add paging), or is there some way to say "sort numeric, treat all strings as positive/negative infinity"?
This discussion has been closed.
Replies
for columns you want this sorting on, set sType to "numeric_ignore_nan" and paste in the not-a-number routines below (non-numbers always sorted to bottom).
[untested, but should work, or need minor tweaking if I mixed up the negative and positive 1's]
[code]
jQuery.fn.dataTableExt.oSort['numeric_ignore_nan-asc'] = function(x,y) {
if (isNaN(x) && isNaN(y)) return ((x < y) ? 1 : ((x > y) ? -1 : 0));
if (isNaN(x)) return 1;
if (isNaN(y)) return -1;
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numeric_ignore_nan-desc'] = function(x,y) {
if (isNaN(x) && isNaN(y)) return ((x < y) ? 1 : ((x > y) ? -1 : 0));
if (isNaN(x)) return 1;
if (isNaN(y)) return -1;
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
[/code]