balicekt
balicekt

Reputation: 613

Angular ng-repeat sorting object of objects

$scope.openingtimes= {
    "1": {"id": 1, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
    "5": {"id": 5, "day": 1, "name_of_day": "Pondělíadas", "open_from": "13:00", "open_to": "16:02"},
    "7": {"id": 7, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
    "9": {"id": 9, "day": 1, "name_of_day": "Pondělísf", "open_from": "13:00", "open_to": "16:00"},
    "10": {"id": 10, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
    "11": {"id": 11, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
    "12": {"id": 12, "day": 1, "name_of_day": "Pondělí", "open_from": "13:02", "open_to": "16:00"},
    "13": {"id": 13, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"}
};

problem with sorting because in view:

<div ng-repeat="time in openingTimes >
{{ time.id }}
</div>

Im getting this order: 1, 10, 11, 12, 13, 5, 7, 9 and this is ofcourse wrong. I can not change object it has to be same I know solution is make this:

 $scope.openingtimes= {
        1: {"id": 1, "day": 1, "name_of_day": "Pondělí", "open_from": "13:00", "open_to": "16:00"},
        5: {"id": .....

but I can not do it. I tried something like:

<div ng-repeat="time in openingTimes | orderBy:'id*1'">

but it doesn't work. Doues anyone know how to make it sort like numbers? Thank you very much.

Upvotes: 1

Views: 823

Answers (1)

balicekt
balicekt

Reputation: 613

I finally found solution and it is how pankajparkar said filter like this:

    app.filter('orderObjectBy', function() {
    return function(items, field, reverse) {
        var filtered = [];
        angular.forEach(items, function(item) {
            filtered.push(item);
        });
        filtered.sort(function (a, b) {
            return (a[field] > b[field] ? 1 : -1);
        });
        if(reverse) filtered.reverse();
        return filtered;
    };
});

and VIEW:

<div ng-repeat="time in openingTimes | orderObjectBy:'id':false">

thanks for help!

Upvotes: 2

Related Questions