Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using angular orderby with the custom function. It looks like this:

Objects:

$scope.array = [{'field1': some_value,
                'field2': some_value,
                    ...
                'field10': some_value},
                    ...
                {'field1': some_value,
                    ...
                'field10': some_value} // Field can be more in the future
               ]

Controller:

$scope.array = $filter('orderBy')($scope.array, [function(item) {
    return customOrderFunction(item);
}, 'field1', 'field2', 'field3', 'field4', 'field5',...,'field10']);

But it's not pretty && the field can be more in the future. I try to make a string to reduce the code size.

var fieldString = ['field1', 'field2', ... 'field10'];

$scope.array = $filter('orderBy')($scope.array, [function(item) {
    return customOrderFunction(item);
}, fieldString.join()]);

But it gives an error. Is there any possible way to achieve this with orderBy? Or is there possible way to do this with JavaScript?

share|improve this question
1  
Is there any reason you are not using plain old JS array sort with a custom sort function instead? orderBy doesn't appear to have any special behaviour that you can't do with sort. –  miqid Aug 1 '14 at 0:38
    
I see. Then I am going to use sort()... :( –  Kimchi Man Aug 1 '14 at 12:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.