noobcoder noobcoder - 4 months ago 15
AngularJS Question

AngularJS orderby not working as expected

I am using Angular directive to orderBy on table rows, with the data coming as an array of objects from a service.

<tr ng-repeat="album in vm.albums | orderBy: album.data.title">

<td>{{album.data.title}}</td>
<td>{{album.data.date}}</td>
<td>{{album.data.type}}</td>
<td>{{album.data.username}}</td>
</tr>


Here's what my
vm.albums
array looks like:

vm.albums ---> [>Object1, >Object2, >Object3......]


And each object looks like the foll:

Object1 ---> {slug: "my-title", data: Object}
Object2 ---> {slug: "beta-title", data: Object}
Object3 ---> {slug: "alpha-title", data: Object}


And the object
Object1.data
or any object in the vm.albums array looks like the following:

Object1.data ---> {title: "My Title", date: "2015-04-06", type: "Blue", username: 'rust'}
Object2.data ---> {title: "Beta Title", date: "2012-04-07", type: "Orange", username: 'badname'}
Object3.data ---> {title: "Alpha Title", date: "2013-09-06", type: "Lemons", username: 'xerox'}


I still don't get the right sorted
<td>
in my table using the
orderBy
I am using. Not sure what I am doing wrong. Any clue ?

Answer

try

<tr ng-repeat="album in vm.albums | orderBy: 'data.title'">
  <td>{{album.data.title}}</td>
  <td>{{album.data.date}}</td>
  <td>{{album.data.type}}</td>
  <td>{{album.data.username}}</td>
</tr>