tristen / tablesort

:arrow_up_down: A small tablesorter in plain JavaScript

Home Page:https://tristen.ca/tablesort/demo/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Columns of numbers mixed with dashes

mauvilsa opened this issue · comments

It is very common to have tables in which columns are a mix of numbers and non-numbers, for example dashes, because for some reason a value for a row might not be available. An example can be found at https://jsfiddle.net/mauvilsa/fb6wgd8t/. If you click on the R column header multiple times, the 100 value correctly goes to the beginning at the end because all of the values are numbers. However, if you do the same for the F1 column which includes a dash, the sort is not done for numbers, so the order is incorrect.

Note that in general this kind of columns is very common and not just mixing with dashes. It could also be something like N/A or NaN. There could also be a mix with infinites as Inf and -Inf.

Is there any way to make number sorting work for these kind of columns? Maybe a way to force columns to be sorted numerically?

👋 You can use a data-sort attribute to normalize values to a consistent data type. http://tristen.ca/tablesort/demo/#override-data-that-is-sorted-on

@tristen Thank you for the response. Though looking at the demo what I need is not data-sort, but to add data-sort-method to the number mixed number columns as <th data-sort-method="number">F1</th>.