使用带有数组&的Angular'orderBy'过滤器功能

时间:2014-08-04 20:06:17

标签: javascript angularjs sorting angularjs-orderby

我要排序的对象数组如下所示:

var students = [
                {'name': 'AAA',
                'year': 'freshman',
                'score': 90,},

                {'name': 'ABA',
                'year': 'sophomore',
                'score': 100},
                ...
                ];

自定义排序功能:

function customOrder(value) {
    switch(value){
        case 'freshman':
            return 0;
        case 'sophomore':
            return 1;
        case 'junior':
            return 2;
        case 'senior':
            return 3;
    }
}

我正在使用Angular orderBy过滤器:

students = $filter('orderBy')(students, function(item) {
    return customOrder(item.year);
};

使用上面的代码,我可以按年份对学生数组进行排序。但我想按如下方式对数组进行排序:年:DESC ,然后名称:ASC ,然后得分:DESC

我为此做了一个数组:

$scope.sortArray = ['name', '-score'];

我改变了这样的排序逻辑:

students = $filter('orderBy')(students, [function(item) {
    return customOrder(item.year);
}, $scope.sortArray]); // I don't want to hard code here (i.e. 'name', '-score')

但它给出了一个错误。

有没有办法对学生阵列进行排序?

0 个答案:

没有答案
相关问题