Orderby not working with dict syntax on ng-repeat

Cameron Singe picture Cameron Singe · Jan 15, 2013 · Viewed 29.3k times · Source

I am trying to use ng-repeat with a dictionary style syntax and apply an order to the key value.

(key, value) in something | orderBy:'key'

It seems OrderBy isn't working as expected

Example here http://jsfiddle.net/mhXuW/

Answer

Mark Rajcok picture Mark Rajcok · Jan 15, 2013

The parameters to orderBy must match property names in an array of objects.

Your data needs to look something like this:

$scope.list2 = [ { id:"2013-01-08T00:00:00", name:'Joe'},
                 { id:"2013-01-09T00:00:00", name:'Sue'}];

Then a filter like this will work:

<div ng-repeat="item in list2 | orderBy:'id':true">

Fiddle.

Note that orderBy works on the entire array (something in your sample code above) and it returns a sorted array. orderBy doesn't know anything about key and value.