1

The array of objects that I want to sort looks like this:

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

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

Custom sorting function:

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

And I'm using the Angular orderBy filter:

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

With the above code, I can sort the students array by year. But I want to sort the array like this: year: DESC, then name: ASC, then score: DESC.

I made an array for this:

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

And I changed the sorting logic like this:

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

But it gives an error.

Is there any possible way to sort the students array?

Kimchi Man
  • 1,131
  • 3
  • 13
  • 24
  • Check out this thread: http://stackoverflow.com/questions/12040395/angularjs-custom-sort-function-in-ng-repeat – orange Aug 04 '14 at 21:01

0 Answers0