排序表中的数组顺序

时间:2019-03-30 21:40:40

标签: javascript html arrays

我想创建一个脚本,该脚本根据您使用鼠标单击的列标题对HTML表进行排序。我正在尝试执行此处给出的答案:

https://stackoverflow.com/a/14267838/330663

我做了一些修改,所以现在排序功能如下:

var COLUMNS_NUMBER = 4;

var myArrayColumns = [3,2,1,0];

function sortDataTable(arrayColumns, asc)
{
    for (var i = 0, n = COLUMNS_NUMBER; i < n; i++)
    {
//      var thisColumn = arrayColumns[i];
        var thisColumn = arrayColumns.reverse()[i];
        DATA_TABLE = DATA_TABLE.sort(function(a,b)
        {
            if (asc)
            {
                return (a[thisColumn].toLowerCase() > b[thisColumn].toLowerCase()) ? 1 : -1;
            }
            else
            {
                return (a[thisColumn].toLowerCase() < b[thisColumn].toLowerCase()) ? 1 : -1;
            }
        });
    }
}

我这样调用函数:

sortDataTable(myArrayColumns, true);

其中3、2、1、0是数据中的列号。

尽管我颠倒了myArrayColumns的顺序,但它似乎总是按照列0进行排序。为什么会这样?

以下是一些示例数据:

var DATA_TABLE =
[
    ["ASCII/Unicode/HTML Codes","Reference","091","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-ascii-unicode-html-codes.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a></span><br/>"],
    ["ActionScript 3.0 Key Codes","Reference","092","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-actionscript-3-0-key-codes.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a></span><br/>"],
    ["Age of Decadence, The","Roleplaying","156","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-age-of-decadence-the.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a></span><br/>"],
    ["Aliens versus Predator","Action","002","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-aliens-versus-predator.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a></span><br/>"],
    ["Allegiance","Simulation","014","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-allegiance.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a></span><br/>"],
    ["American McGee's Alice","Action","003","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-american-mcgees-alice.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a></span><br/>"],
    ["Anachronox","Roleplaying","140","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-anachronox.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a></span><br/>"],
    ["Apple Virtual Key Codes","Reference","090","Mac: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-apple-virtual-key-codes.php?sty=15&lay=5&fmt=0\">US 109 Key (A1048)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-apple-virtual-key-codes.php?sty=15&lay=6&fmt=0\">US 109 Key (A1243)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-apple-virtual-key-codes.php?sty=15&lay=7&fmt=0\">UK 110 Key (A1048)</a></span><br/>"],
    ["Blank Starter","Reference","184","PC: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=1&fmt=0\">US 104 Key (ANSI)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=3&fmt=0\">DE 105 Key (ISO)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=4&fmt=0\">FR 105 Key (ISO)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=8&fmt=0\">UK 105 Key (ISO)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=9&fmt=0\">ES 105 Key (ISO)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=10&fmt=0\">US 104 Key (Dvorak)</a></span><br/>Mac: <span style=\"font-size:smaller;\"><a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=5&fmt=0\">US 109 Key (A1048)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=6&fmt=0\">US 109 Key (A1243)</a>, <a target=\"_blank\" href=\"./keyboard-diagram-blank-starter.php?sty=15&lay=7&fmt=0\">UK 110 Key (A1048)</a></span><br/>"]
]

谢谢。

1 个答案:

答案 0 :(得分:2)

这可能是一个更简单的主意。

var myArrayColumns = [3, 2, 1, 0];

function sortDataTable(arrayColumns, asc) {
  if (asc) {
    return arrayColumns.sort();
  } else {
    return arrayColumns.sort().reverse();
  }
}

console.log(sortDataTable(myArrayColumns))