joequery/Stupid-Table-Plugin

Numbers with separator

Closed this issue · 2 comments

I have a table that have numbers with separators (as 1,000 or 1.000 for thousand). I tryed data-sort="float", but it doesn't work if there is a number lower than a thousand.

Is there some way to do that already? If not, I'll make a pull request. I looked into it, but I've found nothing.

You can incorporate data-sort-value (as described here). Have the data-sort-value be the floating point value of that number.

I've found a simpler solution, but I didn't make a pull request because I think it wasn't in your plans for this plugin.

At my country we use _dots as thousand's separator_ and comma for floating numbers. So I did this:

$.fn.stupidtable.default_sort_fns = {
    "int-separated": function(a, b) {
        return parseInt(a.replace(/\./g,''), 10) - parseInt(b.replace(/\./g,''), 10);
    },
    "float-separated": function(a, b) {
        return parseFloat(a.replace(/\./g,''), 10) - parseFloat(b.replace(/\./g,''), 10);
    }
};

If someone reading this wants to reorder numbers with _commas as thousand's separator_, you can use something like that:

$.fn.stupidtable.default_sort_fns = {
    "int-separated": function(a, b) {
        return parseInt(a.replace(/,/g,''), 10) - parseInt(b.replace(/,/g,''), 10);
    },
    "float-separated": function(a, b) {
        return parseFloat(a.replace(/,/g,''), 10) - parseFloat(b.replace(/,/g,''), 10);
    }
};