【问题标题】:jQuery can't order numbers with thousands dot separated orderingjQuery 不能以千点分隔的顺序对数字进行排序
【发布时间】:2016-11-29 14:20:06
【问题描述】:

使用以下 jQuery 代码对表格行进行排序,千位格式的带有点的数字排序不正确:

这些数字:

1.000、2.500、4.000、850

排序为:

850、4.000、2.500、1.000

我需要订购这些样品编号而不删除点。

jQuery 代码:

$('th').each(function (column) {
    $(this).addClass('sortable').click(function () {
        var findSortKey = function ($cell) {
            return $cell.find('.sort-key').text().toUpperCase()+ ' ' + $cell.text().toUpperCase();

        };
        var sortDirection = $(this).is('.sorted-asc') ? -1 : 1;
        var $rows = $(this).parent().parent().parent().find('tbody tr').get();
        var bob = 0;
        // Loop through all records and find
        $.each($rows, function (index, row) {
            row.sortKey = findSortKey($(row).children('td').eq(column));
        });

        // Compare and sort the rows alphabetically or numerically
        $rows.sort(function (a, b) {
            if (a.sortKey.indexOf('-') == -1 && (!isNaN(a.sortKey) && !isNaN(a.sortKey))) {
                 // Rough Numeracy check

                    if (parseInt(a.sortKey) < parseInt(b.sortKey)) {
                        return -sortDirection;
                    }
                    if (parseInt(a.sortKey) > parseInt(b.sortKey)) {
                        return sortDirection;
                    }

            } else {
                if (a.sortKey < b.sortKey) {
                    return -sortDirection;
                }
                if (a.sortKey > b.sortKey) {
                    return sortDirection;
                }
            }
            return 0;
        });

        // Add the rows in the correct order to the bottom of the table
        $.each($rows, function (index, row) {
            $('tbody').append(row);
            row.sortKey = null;
        });

        // Identify the column sort order
        $('th').removeClass('sorted-asc sorted-desc');
        var $sortHead = $('th').filter(':nth-child(' + (column + 1) + ')');
        sortDirection == 1 ? $sortHead.addClass('sorted-asc') : $sortHead.addClass('sorted-desc');

        // Identify the column to be sorted by
        $('td').removeClass('sorted').filter(':nth-child(' + (column + 1) + ')').addClass('sorted');
    });
});

【问题讨论】:

  • 这就是为什么您从不对格式化或本地化的日期、数字等进行排序。
  • 显然1.000 小于850
  • @PraveenKumar 您标记为重复的问题是关于数据表的。这是一个自定义函数,而不是插件。该解决方案不符合我的要求。
  • @user3472675 重新打开。
  • 没有接受的答案,但看看这个相关/重复的问题:stackoverflow.com/questions/25645163/…

标签: jquery


【解决方案1】:

在 JavaScript 上,千位数字没有分隔符。仅用于十进制,它是 .

使用点分隔符,您不能将数字变量定义为 Intfloat,因为这将被视为十进制数,并且会删除小数点后的零。

您仍然可以使用点对您的号码进行排序。

  • 将点替换为空值 ("")
  • 给你的号码排序
  • 并将点分隔符添加为字符串

请尝试:

var numbers = ["4.500", "1.000", "1", "5", "25", "10"];
var num = [];

$.each(numbers,function(i,e){
  dotPos = numbers[i].replace(/\./g,"")
  num.push(parseInt(dotPos))
})
num.sort(function(a, b){return a-b});
$.each(num,function(i,e){
  console.log(num[i].toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1."))
})
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

    猜你喜欢
    • 2015-09-27
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多