3

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 ?

Community
  • 1
  • 1
Sumama Waheed
  • 3,579
  • 3
  • 18
  • 32

5 Answers5

3

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/

Sumama Waheed
  • 3,579
  • 3
  • 18
  • 32
Mike Feltman
  • 5,160
  • 1
  • 17
  • 38
1

You could write your own custom order function. It would take a parameter - your card - and return either of those fields.

JS:

    $scope.sort = function(card) {
        return card.actualStartDate !== undefined ? card.actualStartDate : card.estimatedStartDate;
    }

HTML:

<div ng-repeat="card in myCards | orderBy:sort">...</div>
John Smith
  • 2,291
  • 4
  • 22
  • 33
0

https://stackoverflow.com/a/25116296/2474688
Try this and see if it works. Just going through this thread it looks like they are sorting based upon the index via a custom filter.

Community
  • 1
  • 1
jsmtslch
  • 708
  • 7
  • 19
0

You can use custom OrderBy sorting :

<tr ng-repeat="contact in contacts | orderBy:randomSort"> 

$scope.randomSort = function(contact) {
  // your condition
};
Kais
  • 1,925
  • 3
  • 23
  • 35
0

By my experience, Angular ngRepeat order does not like undefined values, so adding missing members as null fixes your issue.

By the way, you do not need to mention "doc". Just writing the member is enough.

Also, writing custom sorting method consumes client resources a lot.

Here is the edited fiddle which works correctly: http://jsfiddle.net/g201tudg/2/

  • thank i removed the doc. , it was part of my custom code. Your fiddle doesn't work. I should see 1 2 3 4 5 since B = 1, E = 2 , etc – Sumama Waheed May 17 '16 at 16:10