排序'人类'表分类器中的数字

Sorting 'human' numbers in tablesorter

本文关键字:数字 人类 排序 分类器      更新时间:2023-09-26

我正在使用表分类器对一组值进行排序,这些值的格式从710,2316.39 million37.3 million5.3 million等等都有所不同。目前,插件排序只是忽略million,否则会很好地排序,但因此您会得到类似"530万、639万、3730万、710231、"的内容

您需要添加一个解析器来用它们的值替换名称(演示):

// see big list here: http://www.unc.edu/~rowlett/units/large.html
var numbers = {
    'zero'        : 0,
    'hundred'     : 100,
    'thousand'    : 1e3,
    'million'     : 1e6,
    'billion'     : 1e9,
    'trillion'    : 1e12,
    'quadrillion' : 1e15,
    'quintillion' : 1e18,
    'sextillion'  : 1e21,
    'septillion'  : 1e24,
    'octillion'   : 1e27,
    'nonillion'   : 1e30,
    'decillion'   : 1e33
};
$.tablesorter.addParser({
    id: "namedNumbers",
    is: function () {
        return false;
    },
    format: function (s, table) {
        var v,
            result = 1,
            arry = (s || '').split(/['-'s]+/),
            len = arry.length;
        while (len) {
            v = $.tablesorter.formatFloat( (arry[--len] || '').toLowerCase(), table );
            if ( numbers.hasOwnProperty(v) ) {
                result *= numbers[v];
            } else {
                result *= parseFloat(v);
            }
        }
        return result !== 1 ? result : s;
    },
    type: "numeric"
});
$(function () {
    $('table').tablesorter({
        theme: 'blue',
        headers : {
            1 : { sorter : 'namedNumbers' }
        }
    });
});