Search code examples
javascriptangularjssortingangularjs-ng-repeatangularjs-orderby

Angular ng-repeat orderBy multiple fields as one field


My question is not the same as orderBy multiple fields in Angular.

My array has objects where some have

{
  estimatedStartDate : "..."
}

and some have :

{
  actualStartDate : "..."
}

So is there a way to orderBy considering both fields.

orderBy:['estimatedStartDate','actualStartDate'] 

^ This doesn't work.

Here is a fiddle: http://jsfiddle.net/g201tudg/1/

So pretend like both of these fields are the same and then sort ?


Solution

  • Add a function like this to your controller:

    $scope.sortByEitherDate = function(row) {
        if (row.actualStartDate) {
            return row.actualStartDate;
        }
        return row.estimatedStartDate;
    }
    

    Then order by it with orderBy:sortByEitherDate

    working fiddle: http://jsfiddle.net/g201tudg/4/