Javascript用字符串和int

时间:2018-05-24 19:38:35

标签: javascript arrays sorting



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Results</title>
</head>
<body>
  <div>
    <table width="80%" border="0" align="center">
      <tr>
        <td width="40%" colspan="4" align="center">
	    <div style="overflow: scroll; height: 400px; width: 80%; border: 1px dotted black; background-color: #DAAD1F;">
<script>
localStorage.setItem('1526200226958', [1, 'completed', '00:01:06:32', 'Mario,3.png,']);
localStorage.setItem('1526205223958', [1, 'completed', '00:04:03:14', 'Toad,2.png,']);
localStorage.setItem('1526201223958', [2, 'notCompleted', '00:08:09:37', 'Princess,5.png,']);
localStorage.setItem('1526220323958', [12, 'notCompleted', '00:06:01:59', 'Luigi,4.png,']);
localStorage.setItem('1526300323958', [7, 'notCompleted', '00:04:34:37', 'Luigi,4.png,']);
localStorage.setItem('1526600323958', [3, 'notCompleted', '00:13:53:04', 'Luigi,4.png,']);
localStorage.setItem('1526000253958', [1, 'notCompleted', '00:04:02:20', 'Yoshi,1.png,']);
var test = new Array();
var i = 0;
var pegsLeft;
var gameDone = "";
for (var key in localStorage) {
    if (key != 'key' && key != 'setItem' && key != 'getItem' && key != 'removeItem' && key != 'clear' && key != 'length' && key != 'board') {
        test[i] = localStorage.getItem(key).split(',');
        i++;
    }
}
test.sort(mySortingA);

function mySortingA(a, b) {
    a = a[1] + a[0] + a[2];
    b = b[1] + b[0] + b[2];
    return a == b ? 0 : (a < b ? -1 : 1)
}

document.write("<table width='80%' border='0' align='center' style='border-collapse:collapse;'>");
for (i = 0; i < test.length; i++) {
    if (test[i][1] == 1) {
        pegsLeft = " peg"
    } else {
        pegsLeft = " pegs"
    };
    if (test[i][1] == "completed") {
        gameDone = "game completed"
    } else {
        gameDone = " "
    };
    document.write("  <tr"); if (ranking == i) { document.write(" bgcolor='#FFCC00'><a name='actPos'></a>") } else { document.write(">") };
    document.write("    <td width='30'>" + (i + 1) + ".</td>");
    document.write("    <td width='30'>Image: " + test[i][4] + "</td>");
    document.write("    <td>" + test[i][3] + test[i][5] + "</td>");
    document.write("    <td width='15%'>" + test[i][0] + pegsLeft + " left</td>");
    document.write("    <td width='15%'>" + gameDone + "</td>");
    document.write("    <td width='15%'>" + test[i][2] + "</td>");
    document.write("  </tr>");
}
document.write("</table>");
      </script></div></td>
      </tr>
    </table>
  </div>
</body>
</html>
&#13;
&#13;
&#13;

我在localStorage中有以下数据:

localStorage.setItem(&#39; 1526200226958&#39;,[1,&#39;已完成&#39;,&#39; 01:01:06:32&#39;,&#39;马里奥,3 .PNG,&#39;]); localStorage.setItem(&#39; 1526205223958&#39;,[1,&#39;已完成&#39;,&#39; 00:04:03:14&#39;,&#39; Toad,2.png, &#39;]); localStorage.setItem(&#39; 1526201223958&#39;,[2,&#39; notCompleted&#39;,&#39; 00:08:09:37&#39;,&#39; Princess,5.png, &#39;]); localStorage.setItem(&#39; 1526220323958&#39;,[12,&#39; notCompleted&#39;,&#39; 00:06:01:59&#39;,&#39; Luigi,4.png, &#39;]); localStorage.setItem(&#39; 1526300323958&#39;,[7,&#39; notCompleted&#39;,&#39; 00:04:34:37&#39;,&#39; Luigi,4.png, &#39;]); localStorage.setItem(&#39; 1526600323958&#39;,[3,&#39; notCompleted&#39;,&#39; 00:13:53:04&#39;,&#39; Luigi,4.png, &#39;]); localStorage.setItem(&#39; 1526000253958&#39;,[1,&#39; notCompleted&#39;,&#39; 00:04:02:20&#39;,&#39; Yoshi,1.png, &#39;]);

我把这些数据放在一个二维数组中:

for (var key in localStorage) {
   if (key != 'key' && key != 'setItem' && key != 'getItem' && key != 'removeItem' && key != 'clear' && key != 'length' && key != 'board') {
        test[i] = localStorage.getItem(key).split(',');
        i++;
    }
}

然后我尝试使用以下函数对它们进行排序(首先是所有完成游戏的人,然后是第二个数组的第一个值,最后是时间):

function mySortingA(a, b) {
a = a[1] + a[0] + a[2];
b = b[1] + b[0] + b[2];
return a == b ? 0 : (a < b ? -1 : 1)

}

但是条目: localStorage.setItem(&#39; 1526220323958&#39;,[12,&#39; notCompleted&#39;,&#39; 00:06:01:59&#39;,&#39; Luigi,4.png, &#39;]); 被视为错误,似乎它被视为字符串而不是整数。我试图解析第二个数组中的第一个值:

a = a[1] + parseInt(a[0]) + a[2];
b = b[1] + parseInt(b[0]) + b[2];

但它不起作用。谁能告诉我应该怎么做?

谢谢!

3 个答案:

答案 0 :(得分:0)

您可以为同一索引的每个元素获取索引和比较结果。这适用于数字和字符串。

var array = [[1, 'completed', '00:01:06:32', 'Mario,3.png,'], [12, 'notCompleted', '00:10:01:59', 'Luigi,4.png,'], [1, 'completed', '00:04:03:14', 'Toad,2.png,'], [2, 'notCompleted', '00:08:09:37', 'Princess,5.png,'], [12, 'notCompleted', '00:06:01:59', 'Luigi,4.png,'], [24, 'completed', '00:04:34:37', 'Luigi,4.png,'], [3, 'notCompleted', '00:13:53:04', 'Luigi,4.png,'], [1, 'notCompleted', '00:04:02:20', 'Yoshi,1.png,'], [12, 'notCompleted', '00:01:01:59', 'Luigi,4.png,'], [2, 'completed', '00:04:34:37', 'Luigi,4.png,']];

array.sort(function (a, b) {
    var d;
    [1, 0, 2].some(i => d = a[i] > b[i] || -(a[i] < b[i]));
    return d;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

如果连接字符串和整数,结果将是一个字符串。你对字符串中间的数字做了parseInt并不重要。

当您创建自己的排序函数时,结果将得到1,0或-1。首先比较您想要的主要值。如果第一次测试的结果是零,那么你就有了平局。如果是这些情况,你会比较第二个值并返回1或-1作为函数的结果。

答案 2 :(得分:0)

目前,您使用字符串连接数字,因此整个数组按字符串值排序。您需要分别检查每个值:

var test = [
  [1, 'completed', '00:01:06:32', 'Mario,3.png,'],
  [12, 'notCompleted', '00:10:01:59', 'Luigi,4.png,'],
  [1, 'completed', '00:04:03:14', 'Toad,2.png,'],
  [2, 'notCompleted', '00:08:09:37', 'Princess,5.png,'],
  [12, 'notCompleted', '00:06:01:59', 'Luigi,4.png,'],
  [24, 'completed', '00:04:34:37', 'Luigi,4.png,'],
  [3, 'notCompleted', '00:13:53:04', 'Luigi,4.png,'],
  [1, 'notCompleted', '00:04:02:20', 'Yoshi,1.png,'],
  [12, 'notCompleted', '00:01:01:59', 'Luigi,4.png,'],
  [2, 'completed', '00:04:34:37', 'Luigi,4.png,'],
];

test.sort((a, b) => {
  // Sort by the second value
  if (a[1] < b[1]) {
    return -1;
  }

  if (a[1] > b[1]) {
    return 1;
  }

  // If second value is equal - sort by the first value of the array
  if (a[0] < b[0]) {
    return -1
  }

  if (a[0] > b[0]) {
    return 1;
  }

  // If first value is equal - sort by the third value of the array
  if (a[2] < b[2]) {
    return -1;
  }

  if (a[2] > b[2]) {
    return 1;
  }

  // All are equal
  return 0;
});

console.log(test);