【问题标题】:localeCompare not detecting whole number when sortinglocaleCompare 排序时未检测到整数
【发布时间】:2020-05-14 19:24:18
【问题描述】:

我使用 JS 和 jQuery 对我的表格进行排序,这是我对表格进行排序的代码

function sortTable(table, column, order) {
        var asc = order === \'asc\';
        var tbody = table.find(\'tbody\');

        tbody.find(\'tr\').sort(function (a, b) {
            if (asc) {
                return $(\'td:eq(\' + column + \')\', a).text()
                    .localeCompare($(\'td:eq(\' + column + \')\', b).text());
            } else {
                return $(\'td:eq(\' + column + \')\', b).text()
                    .localeCompare($(\'td:eq(\' + column + \')\', a).text());
            }
        }).appendTo(tbody); 
    }

问题是该表仅按第一个数字排序,如下所示:

[ 1, 10, 12, 18, 2, 3, 33, 4, 5]

我希望它是这样的

[ 1, 2 , 3, 4, 5, 10, 12, 18, 33]
  • 您不需要localeCompare 来对数字进行排序。只需在值上使用parseInt 并像往常一样对其进行排序。
  • 你有任何例子如何实现我的功能吗?

标签: javascript jquery laravel


【解决方案1】:

有一个选项对象可以提供给 localeCompare 方法。假设其中一个选项表明您也可以比较数字('1' < '2' < '10')。 基本用法如下所示:

a.localeCompare(b, 'en', {numeric: true})

你的代码可以这样修改

function sortTable(table, column, order) {
        var asc = order === 'asc';
        var tbody = table.find('tbody');

        tbody.find('tr').sort(function (a, b) {
            if (asc) {
                return $('td:eq(' + column + ')', a).text()
                    .localeCompare($('td:eq(' + column + ')', b).text(), 'en', {numeric: true});
            } else {
                return $('td:eq(' + column + ')', b).text()
                    .localeCompare($('td:eq(' + column + ')', a).text(), 'en', {numeric: true});
            }
        }).appendTo(tbody); 
    }

检查链接以找出 localeCompare 方法的可能选项:

Intl.Collator() constructor

String.prototype.localeCompare()

【讨论】:

    【解决方案2】:

    问题是您将数字排序为字符串。你正在这样做:

    arr = [ 1, 5, 10, 12, 18, 2, 3, 33, 4];
    
    arr.sort((a,b) => {
            return a.toString().localeCompare(b.toString());
          });
    //[ 1, 10, 12, 18, 2, 3, 33, 4, 5 ]
    

    你应该这样做:

    arr.sort((a,b) => {
            return a>b;
          });
    //[ 1, 2, 3, 4, 5, 10, 12, 18, 33 ]
    

    因此,您可以将 $(whatever).text() 转换为 int 添加+喜欢+$(whatever).text() 或使用parseInt()

    如果这些是数字$('td:eq(' + column + ')', a).text(),您可以执行以下操作:

    function sortTable(table, column, order) {
            var asc = order === 'asc';
            var tbody = table.find('tbody');
    
            tbody.find('tr').sort(function (a, b) {
                if (asc) {
                    return parseInt($('td:eq(' + column + ')', a).text())>parseInt($('td:eq(' + column + ')', b).text());
                } else {
                    return parseInt($('td:eq(' + column + ')', b).text())
                        <parseInt($('td:eq(' + column + ')', a).text());
                }
            }).appendTo(tbody); 
        }
    

    【讨论】:

    • 当我像你说的那样改变它时,它只对前 2 行进行排序。但它不仅需要扫描整个表的前两行。
    • var arr 是一个例子,可能很大,它是一样的
    • 这些数字是 $('td:eq(' + column + ')', a).text() 吗?
    • 对,他们是。我找到了答案。问题是它被视为字符串而不是数字。
    【解决方案3】:

    因为如果您想将该列视为数字进行更改,则将其视为刺痛,假设column1,或者如果您有多个数字列,那么您可以在数组中检查它,假设是否为@987654323 @

     tbody.find('tr').sort(function (a, b) {
            if (asc) {
                if(column==1){ // if check_col.indexOf(column) > -1 for multiple numeric column
                   return parseInt($('td:eq(' + column + ')', a).text()) - parseInt($('td:eq(' + column + ')', b).text());
                }else{
                   return $('td:eq(' + column + ')', a).text()
                    .localeCompare($('td:eq(' + column + ')', b).text());
                }
            } else {
                if(column==1){ // if check_col.indexOf(column) > -1 for multiple numeric column
                   return parseInt($('td:eq(' + column + ')', b).text()) - parseInt($('td:eq(' + column + ')', a).text());
                }else{
                   return $('td:eq(' + column + ')', b).text()
                    .localeCompare($('td:eq(' + column + ')', a).text());
                }
            }
        }).appendTo(tbody); 
    

    【讨论】:

      【解决方案4】:

      试试这个,我没有直接回答,但这会是你的问题 enter link description here

      var stringArr = [10,"20",null,"1","bar","-2",-3,null,5,"foo"];
      
      
      stringArr.sort(sortByDataString); 
      
      function isNumber(n) {
        return !isNaN(parseFloat(n)) && isFinite(n);
      }
      
      // String sorting function
      function sortByDataString(a, b) {
          if (a === null) {
              return 1;
          }
          if (b === null) {
              return -1;
          }
          if (isNumber(a) && isNumber(b)) {
              if (parseInt(a,10) === parseInt(b,10)) {
                  return 0;
              }
              return parseInt(a,10) > parseInt(b,10) ? 1 : -1;
          }
          if (isNumber(a)) {
              return -1;
          }
          if (isNumber(b)) {
              return 1;
          }
          if (a === b) {
              return 0;
          }
          return a > b ? 1 : -1;
      }
      

      【讨论】:

      • 但是我怎样才能在我的函数中实现它呢?
      【解决方案5】:
      function sortTable(table, column, order) {
          var asc = order === 'asc';
          var tbody = table.find('tbody');
      
          tbody.find('tr').sort(function (a, b) {
              if (asc) {
                   return parseInt($('td:eq(' + column + ')', a).text()) - parseInt($('td:eq(' + column + ')', b).text());
              } else {
                  return parseInt($('td:eq(' + column + ')', b).text()) - parseInt($('td:eq(' + column + ')', a).text());
              }
          }).appendTo(tbody); 
      }
      

      【讨论】:

        猜你喜欢
        • 2013-08-29
        • 2017-10-18
        • 2019-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-01
        • 2020-09-20
        • 1970-01-01
        相关资源
        最近更新 更多