angularjsangularjs-ng-repeatangularjs-orderby

AngularJS | how to orderBy with a multidimensional Object?


I want to get my ng-repeat to be ordered by rank, how would I write the expression?

<div ng-app="myApp" ng-controller="orderCtrl">
<ul>
<li ng-repeat="(key, value) in cars | orderBy: cars[key].rank ">
{{cars[key].longName}}</li>
</ul>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('orderCtrl', function($scope) {
$scope.cars = {                
                "BTC" : {
                    "longName"   :   "Bitcoin",
                    "rank"       :   1
                },
                "BCH" : {
                    "longName"   :   "Bitcoin Cash",
                    "rank"       :   3
                },
                "ETH" : {
                    "longName"   :   "Ethereum",
                    "rank"       :   2
                },
                "ETC" : {
                    "longName"   :   "Ethereum Classic",
                    "rank"       :   15
                },
                "IOTA" : {
                    "longName"   :   "IOTA",
                    "rank"       :   4
                },
                "XRP" : {
                    "longName"   :   "Ripple",
                    "rank"       :   6
                },
                "XVG" : {
                    "longName"   :   "Verge",
                    "rank"       :   5
                }
        }; 
});
</script>

Solution

  • Order by can only be applied to arrays and not objects. You must convert your object to an array or create your own filter : https://justinklemm.com/angularjs-filter-ordering-objects-ngrepeat/