plotly / dash-table-experiments

NO LONGER SUPPORTED - use https://github.com/plotly/dash-table instead
MIT License
174 stars 57 forks source link

Comparator for formated number strings #74

Open julius-datajunkie opened 6 years ago

julius-datajunkie commented 6 years ago

Currently, the sortable=True option only sorts when the columns are in raw number. Any formatting that turns that numbers into string will yield erroneous order.

For instance, 12.5% will be ranked higher than 2.1% when sorted in descending order, because the first character 1 is smaller than 2. To fix this I have locally modified the sort.js file as follow:

import R from 'ramda';

export default function sortRows(rows, sortColumn, sortDirection) {
    function comparer(a, b) {
        x = parseFloat(a[sortColumn]);
        y = parseFloat(b[sortColumn]);
        if (sortDirection === 'ASC') {
            if isNan(x):
                return (a[sortColumn] > b[sortColumn]) ? 1 : -1;
            else:
                return (x > y) ? 1 : -1;
        } else if (sortDirection === 'DESC') {
            if isNan(x):
                return (a[sortColumn] < b[sortColumn]) ? 1 : -1;
            else:
                return (x < y) ? 1 : -1;
        }
    }
    return (sortDirection === 'NONE' ?
        rows : R.sort(comparer, rows.slice(0))
    );
}

Nonetheless, this doesn't seem to fix the problem. Are there any patch planned for this feature?

Thanks