这个问题看起来很愚蠢,但我无法找出我做错了什么。
我正在尝试根据特定索引对嵌套数组进行排序。如果我继续对数组进行排序,数组每次都会不断变化,但事实不应该如此。此外,当数组长度有点小时,这个问题不会重现。
// Code goes here
var app = angular.module("myApp", []);
app.controller("someController", ['$scope', function($scope) {
$scope.AppTitle = "Array sort Issue";
$scope.testArray = [
["2016-04-10T18:30:00.000Z", "b20", 104],
["2016-04-10T18:30:00.000Z", "b20", 81],
["2016-04-10T18:30:00.000Z", "b20", 29],
["2016-04-10T18:30:00.000Z", "b20", 1],
["2016-04-10T18:30:00.000Z", "n0c", 155],
["2016-04-10T18:30:00.000Z", "n0c", 21],
["2016-04-10T18:30:00.000Z", "n0c", 12],
["2016-04-10T18:30:00.000Z", "n0c", 10],
["2016-04-10T18:30:00.000Z", "n0c", 8],
["2016-04-10T18:30:00.000Z", "n0c", 8],
["2016-04-10T18:30:00.000Z", "ecty", 101],
["2016-04-10T18:30:00.000Z", "ecty", 58],
["2016-04-10T18:30:00.000Z", "adgi", 127],
["2016-04-10T18:30:00.000Z", "adgi", 24],
["2016-04-10T18:30:00.000Z", "ath", 77],
["2016-04-10T18:30:00.000Z", "ath", 60],
["2016-04-10T18:30:00.000Z", "hry", 124],
["2016-04-10T18:30:00.000Z", "hry", 8],
["2016-04-10T18:30:00.000Z", "tfan", 132],
["2016-04-10T18:30:00.000Z", "sr", 96],
["2016-04-10T18:30:00.000Z", "hwdg", 59],
["2016-04-10T18:30:00.000Z", "hwdg", 15],
["2016-04-10T18:30:00.000Z", "hwdg", 14],
["2016-04-10T18:30:00.000Z", "hwdg", 6],
["2016-04-10T18:30:00.000Z", "alub", 88],
["2016-04-10T18:30:00.000Z", "ax0", 43],
["2016-04-10T18:30:00.000Z", "ax0", 40],
["2016-04-10T18:30:00.000Z", "ax0", 4],
["2016-04-10T18:30:00.000Z", "ax0", 1],
["2016-04-10T18:30:00.000Z", "voin", 67],
["2016-04-10T18:30:00.000Z", "voin", 19],
["2016-04-10T18:30:00.000Z", "voin", 2],
["2016-04-10T18:30:00.000Z", "p", 87],
["2016-04-10T18:30:00.000Z", "ttm", 65],
["2016-04-10T18:30:00.000Z", "ttm", 18],
["2016-04-10T18:30:00.000Z", "bre", 78],
["2016-04-10T18:30:00.000Z", "bre", 1],
["2016-04-10T18:30:00.000Z", "dew", 41],
["2016-04-10T18:30:00.000Z", "dew", 33],
["2016-04-10T18:30:00.000Z", "cgu", 19],
["2016-04-10T18:30:00.000Z", "cgu", 15],
["2016-04-10T18:30:00.000Z", "cgu", 13],
["2016-04-10T18:30:00.000Z", "cgu", 6],
["2016-04-10T18:30:00.000Z", "cgu", 4],
["2016-04-10T18:30:00.000Z", "cgu", 4],
["2016-04-10T18:30:00.000Z", "cgu", 3],
["2016-04-10T18:30:00.000Z", "cgu", 3],
["2016-04-10T18:30:00.000Z", "cgu", 1],
["2016-04-10T18:30:00.000Z", "cgu", 1],
["2016-04-10T18:30:00.000Z", "cgu", 1],
["2016-04-10T18:30:00.000Z", "an", 69],
["2016-04-10T18:30:00.000Z", "hwo", 69],
["2016-04-10T18:30:00.000Z", "camel", 68],
["2016-04-10T18:30:00.000Z", "mysore", 67],
["2016-04-10T18:30:00.000Z", "power", 60],
["2016-04-10T18:30:00.000Z", "power", 2],
["2016-04-10T18:30:00.000Z", "www", 49],
["2016-04-10T18:30:00.000Z", "www", 10],
["2016-04-10T18:30:00.000Z", "hgyp", 35],
["2016-04-10T18:30:00.000Z", "hgyp", null],
["2016-04-10T18:30:00.000Z", "hgyp", 4],
["2016-04-10T18:30:00.000Z", "igne", 43],
["2016-04-10T18:30:00.000Z", "igne", 13],
["2016-04-10T18:30:00.000Z", "weight", 54],
["2016-04-10T18:30:00.000Z", "hbk", 50]
]
$scope.sortTable = function() {
$scope.testArray.sort(function(a, b) {
if (a[1] > b[1])
return 1;
if (b[1] > a[1])
return -1;
return 0;
});
}
}]);
添加了 plunker @ https://plnkr.co/edit/B99a28LBGLojcDtuBM2i 上的代码
向下滚动并单击 sortMe 按钮。您可以在每次单击按钮时看到表格行的变化。
提前致谢。你会结束我的痛苦;)
最佳答案
为了稳定排序,您需要另一个参数,例如第三个元素。
排序仅使用部分数据进行检查,例如您的回调。如果一次检查返回 0
,由于内容相同,排序算法不知道 a 是否在 b 之前或相反。在这种情况下,我们需要另一个属性来决定结果应该在哪个范围内出现。欲了解更多详细信息,请访问Wikipedia .
$scope.testArray.sort(function (a, b) {
return a[1].localeCompare(b[1]) || a[2] - b[2];
});
关于Javascript - 嵌套数组排序无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37744450/