The issue looks quite silly, but i am unable to find out what am i doing wrong.
I am trying to sort an Nested array based on a particular index. If i keep sorting the array, array keeps getting changed each time which shouldn't be the case. Moreover, this issue is not getting reproduced when the array length is bit small.
// 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;
});
}
}]);
Added the Code on plunker @ https://plnkr.co/edit/B99a28LBGLojcDtuBM2i
Scroll down and click the sortMe button. You can see the change in table rows on each button click.
Thanks in Advance. You would putting a stop to my misery ;)