【问题标题】:JavaScript case insensitive sorting for HTML tableHTML表格的JavaScript不区分大小写排序
【发布时间】:2022-01-14 08:38:53
【问题描述】:

我只想在这个 Javascript 代码中为我的表实现大小写不敏感排序。 这怎么可能?

请看下面:

cPrev = -1; 
            
function sortBy(c) {
    rows = document.getElementById("data_table").rows.length; 
    columns = document.getElementById("data_table").rows[0].cells.length; 
    arrTable = [...Array(rows)].map(e => Array(columns)); 
    for (ro=0; ro<rows; ro++) {
        for (co=0; co<columns; co++) {
            arrTable[ro][co] = document.getElementById("data_table").rows[ro].cells[co].innerHTML;
        }
    }
    
}

【问题讨论】:

标签: javascript html html-table


【解决方案1】:

要使其不区分大小写,只需使用 .toLowerCase() 或 .toUpperCase()

document.getElementById("data_table").rows[ro].cells[co].innerHTML.toUpperCase()

【讨论】:

    【解决方案2】:

    此函数用于按字母顺序对字符串进行排序,并将大写放在小写之前。

    function (a, b) {
       var x = String(a).toLowerCase(); 
       var y = String(b).toLowerCase(); 
         
       if (x > y) 
         return -1;
       if (x < y) 
         return 1; 
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 1970-01-01
      • 2019-02-06
      • 2019-01-31
      • 2020-09-13
      • 2012-06-22
      • 1970-01-01
      相关资源
      最近更新 更多