Angular.js: 如何使用 orderby 对对象进行排序

Angular.js: How to order objects with orderby

这是我的 angular 代码:

    <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;
            }

我想按每个人的年龄排序,所以我使用了自定义排序功能。但这好像不行。

所以我的代码有什么问题?我该如何解决?

来自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.

看看这个答案:

可能会有帮助。