Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Is it possible to manipulate and sort multiple column in angularjs filter orderBy?

Fiddle

JS

var myApp = angular.module('myApp', []);

function MyCtrl($scope, $filter) {
    var r = [
        {'group':1,'sub':1, 'date': '11/4/2014 2:01 PM'}, 
        {'group':2,'sub':10, 'date': '11/4/2014 4:36 PM'}, 
        {'group':1,'sub':2, 'date': '11/4/2014 8:59 AM'},
        {'group':1,'sub':20, 'date': '4/14/2015 5:54 PM'},
        {'group':2,'sub':1, 'date': '11/3/2014 9:09 PM'},
        {'group':2,'sub':11, 'date': '9/4/2014 2:48 PM'}
    ];

    $scope.mcards = $filter('orderBy')(r, [function(a){
        return -new Date(a.date).getTime(); 
        //manipulate the string and then sort it by ascending or descending order.
    }]);
}

It is not working as expected whereas when I try it in the following way it works great,

$scope.mcards = $filter('orderBy')(r, ['group', function(a){
    return -a.sub;
}]);

What is the problem here, am I missing something?

share|improve this question
    
Any idea or help..!!??? – aravindtrue May 20 at 11:26

1 Answer 1

delete the minus symbol form return in the filter

 var myApp = angular.module('myApp', []);

function MyCtrl($scope, $filter) {
    var r = [
        {'group':1,'sub':1, 'date': '11/4/2014 2:01 PM'}, 
        {'group':2,'sub':10, 'date': '11/4/2014 4:36 PM'}, 
        {'group':1,'sub':2, 'date': '11/4/2014 8:59 AM'},
        {'group':1,'sub':20, 'date': '4/14/2015 5:54 PM'},
        {'group':2,'sub':1, 'date': '11/3/2014 9:09 PM'},
        {'group':2,'sub':11, 'date': '9/4/2014 2:48 PM'}
    ];

    $scope.mcards = $filter('orderBy')(r, [function(a){        
        return new Date(a.date).getTime();
    }],true);

    $scope.cards = $filter('orderBy')(r, ['group', function(a){
        return a.sub;
    }],true);
}

if you will sort reverse sort, add true at last param in filter

see jsfiddle update http://jsfiddle.net/br5sssv7/6/

share|improve this answer
    
Thanks for the help. This code will either sort all the columns in asc or desc. But in my case, I may sort 2 columns in asc and other columns in desc. – aravindtrue May 20 at 9:18

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.