1

Here is my angular code:

    <li ng-repeat="(key, value) in list | orderBy:customOrderFn">
        <span>{{key}}: {{value.age}}</span>
    </li>

        app.controller('ListController', function ($scope) {
            $scope.list = {
                'name1': {
                    age: 22
                },
                'name2': {
                    age: 21
                },
                'name3': {
                    age: 23
                }
            }

            $scope.customOrderFn = function (person) {
                console.log(person);
                return person.age;
            }

I want to order by each person's age, so I using a custom order function. But this seems doesn't work.

So what's is wrong with my code? How can I fix it?

hh54188
  • 14,887
  • 32
  • 113
  • 184
  • 1
    javascript objects don't have order ... use arrays. Using unique property names is more trouble than it's worth anyway for most of what you will be doing with this data – charlietfl Aug 03 '15 at 00:50

1 Answers1

1

From the AngularJS Docs:

Orders a specified array by the expression predicate. It is ordered alphabetically for strings and numerically for numbers. Note: if you notice numbers are not being sorted as expected, make sure they are actually being saved as numbers and not strings.

Have a look at this answer: https://stackoverflow.com/a/12041694/2803660

It might help.

Community
  • 1
  • 1
MrHaze
  • 3,786
  • 3
  • 26
  • 47