Angular orderBy:'timestamp' putting things in the wrong order

5.5k views Asked by At

What I want to do:

Take an array of objects, each object with object.timestamp in milliseconds, and order them from newest to oldest.

The problem:

Angular's orderBy doesn't seem to be putting things in the correct order. The following timestamps are getting put in the following order:

  1. 1416187808218 - // Nov/16/2014 5:30:PM
  2. 1416187881192 - // Nov/16/2014 5:31:PM
  3. 1416189118263 - // Nov/16/2014 5:51:PM
  4. 1416189138827 - // Nov/16/2014 5:52:PM
  5. 1416130064119 - // Nov/16/2014 1:27:AM

The correct/desired order is obviously be:

  1. 1416189138827 - // Nov/16/2014 5:52:PM
  2. 1416189118263 - // Nov/16/2014 5:51:PM
  3. 1416187881192 - // Nov/16/2014 5:31:PM
  4. 1416187808218 - // Nov/16/2014 5:30:PM
  5. 1416130064119 - // Nov/16/2014 1:27:AM

So it's going 4, 3, 2, 1, 5 when it should be, obviously, 1, 2, 3, 4, 5


The relavent code:

html:

<div class="cardWrapper" ng-repeat="card in cards | orderBy:'timestamp'">
  various child elements
</div>

javascript:

scope.cards = homeData.get().cards; // returns an array of objects
// ^this^ becomes something similar to
scope.cards = [
  {text: 'some text...', timestamp: 1416189138827, other: 'data'},
  {text: 'some text...', timestamp: 1416187881192, other: 'data'}
  //etc...
];
2

There are 2 answers

0
mediaguru On

Is your timestamp coming through your API as a string and not an integer? I realize here it looks fine.

0
Maulik On

Try converting timestamp to date and then sort using orderBy() in angularjs.