Obj to array for custom sort filter

by Laxmikant Dange

HTML

<div ng-app="myApp">
    <div ng-controller="myController">
        <div ng-repeat="a in data|mysortFilter">
            {{a.key}} -- {{a.value}}
        </div>
    </div>
</div>

JavaScript

var myApp=angular.module('myApp',[]);
myApp.controller("myController",function($scope){
    
    $scope.data={
        a:{value:10},
        z:{value:24},
        c:{value:3},
        b:{value:12}
    };
});
myApp.filter('mysortFilter',function(){
    return function(obj){
        var array = [];
        Object.keys(obj).forEach(function (key) {
          // inject key into each object so we can refer to it from the template
          obj[key].key = key;
          array.push(obj[key]);
        });
         array.sort(function (a, b) {
          return a.key>b.key;
        });
        console.log(array)
        return array;
    }
});