简体   繁体   English

AngularJS对对象数组进行排序

[英]AngularJS sort an objects array

I'm trying to sort the next array but every time it gives me an error, I've looked for other solutions here in stack overflow but none of them seems to work. 我正在尝试对下一个数组进行排序,但是每次它给我一个错误时,我都在这里在堆栈溢出中寻找其他解决方案,但是它们似乎都不起作用。

$scope.builds = [build:[
    {'_id': '6384', 'name': Mock1, 'builDate': 20150302T110232},
    {'_id': '6383', 'name': Mock2, 'builDate': 20150209T130212},
    {'_id': '6382', 'name': Mock3, 'builDate': 20150103T145212}]
];
    var sorted = $scope.sortByKey($scope.builds.build[index],'builDate')

And this is my function: 这是我的功能:

    $scope.sortByKey = function (array, key) {
        return array.sort(function(a,b){
            var x = a[key] < b[key]? -1:1;
            return x;
        });
    }

$scope.builds is an array not an object, and index doesn't exist...try this: $ scope.builds是一个数组而不是对象,并且索引不存在...请尝试以下操作:

$scope.builds = {build:[
    {'_id': '6384', 'name': Mock1, 'builDate': 20150302T110232},
    {'_id': '6383', 'name': Mock2, 'builDate': 20150209T130212},
    {'_id': '6382', 'name': Mock3, 'builDate': 20150103T145212}]
};
var sorted = $scope.sortByKey($scope.builds.build,'builDate')

There are multiple issues with this. 这有多个问题。

1) You are trying to use an array like an object 1)您正在尝试使用类似对象的数组

$scope.builds = [build:[

...should be... ...应该...

$scope.builds = {build:[

2) 20150302T110232 is not a valid number (the T isn't allowed). 2) 20150302T110232不是有效的数字(不允许使用T )。 So you need to turn those into strings (or turn them into actual Date objects, since that is what they appear to be). 因此,您需要将它们转换为字符串(或将其转换为实际的Date对象,因为这看上去确实如此)。

3) $scope.builds.build[index] is an object, not an array. 3) $scope.builds.build[index]是一个对象,而不是数组。 So, I'm guessing you meant $scope.builds.build instead. 因此,我猜您的意思是$scope.builds.build

var sorted = $scope.sortByKey($scope.builds.build[index],'builDate')

...should be... ...应该...

var sorted = $scope.sortByKey($scope.builds.build,'builDate')

Its not 100% clear that this is the problem from your question, but you're declaring two levels of array at present. 目前还不是100%清楚这是您提出的问题,但是目前您要声明两个级别的数组。

Change the $scope.builds definition to: $scope.builds定义更改为:

$scope.builds = {build:[
    {'_id': '6384', 'name': Mock1, 'builDate': 20150302T110232},
    {'_id': '6383', 'name': Mock2, 'builDate': 20150209T130212},
    {'_id': '6382', 'name': Mock3, 'builDate': 20150103T145212}]
};

Worth noting as well that there are three values (0, 1, and -1) that can be returned from Array.prototype.sort . 还要注意,可以从Array.prototype.sort返回三个值(0、1和-1)。 The sort evaluation needs to return the correct value or the sort will be unstable (and you may get an error). 排序评估需要返回正确的值,否则排序将不稳定(并且您可能会得到错误)。

Your sort function needs to look like this: 您的排序功能应如下所示:

    return array.sort(function(a,b){
        if(a[key] == b[key]) { 
          return 0; 
        }

        return a[key] < b[key]? -1:1;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM