Resolving Angular's ng-repeat orderBy Issues with Objects

Dec 07, 2025 · Programming · 12 views · 7.8

Keywords: AngularJS | orderBy | ng-repeat | JavaScript | JSON

Abstract: This article explores why AngularJS's orderBy filter fails with JSON objects and provides solutions to convert objects to arrays or implement custom filters for sorting. Based on community answers, it offers step-by-step guidance and code examples.

Introduction

AngularJS is a powerful framework for building dynamic web applications, but developers often encounter issues when using the orderBy filter with ng-repeat on JSON objects. This article addresses the common problem where orderBy does not work as expected with objects, based on community insights.

The Limitation of orderBy Filter

According to the AngularJS documentation, the orderBy filter is designed to work exclusively with arrays. When applied to an object, it is ignored because objects in JavaScript do not have a guaranteed order of properties. The ng-repeat directive iterates over the keys of an object, but orderBy cannot sort these keys effectively without converting the object to an array.

Converting Objects to Arrays

To enable sorting, one solution is to convert the JSON object into an array. This can be achieved by creating a custom filter that transforms the object's values into an array. For example, a filter named object2Array can be implemented to push each property value into an array, which can then be sorted using orderBy.

app.filter('object2Array', function() {
  return function(input) {
    var out = [];
    for (var key in input) {
      if (input.hasOwnProperty(key)) {
        out.push(input[key]);
      }
    }
    return out;
  };
});

In the HTML, this filter can be chained with orderBy:

<div ng-repeat="release in releases | object2Array | orderBy:'environment_id'">{{release.environment_id}}</div>

Custom Filter Implementation

Alternatively, a more comprehensive custom filter can be created to directly sort objects. This filter, such as orderObjectBy, takes a field name and optional reverse flag to sort the object's values.

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

Usage in HTML:

<ul>
<li ng-repeat="item in items | orderObjectBy:'color':true">{{ item.color }}</li>
</ul>

Code Example

Here is a complete example integrating the concepts. Assume an Angular module myApp with a controller Main that has a releases object similar to the one in the question.

<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
  <script>
    var app = angular.module('myApp', []);
    app.filter('object2Array', function() {
      return function(input) {
        var out = [];
        for (var key in input) {
          if (input.hasOwnProperty(key)) {
            out.push(input[key]);
          }
        }
        return out;
      };
    });
    app.controller('Main', ['$scope', function($scope) {
      $scope.releases = { /* JSON data here */ };
    }]);
  </script>
</head>
<body ng-controller="Main">
  <div ng-repeat="release in releases | object2Array | orderBy:'environment_id'">
    {{ release.environment_id }} - {{ release.date }}
  </div>
</body>
</html>

Replace the JSON data with the actual object from the question. This setup ensures that the orderBy filter works correctly by first converting the object to an array.

Conclusion

In summary, the orderBy filter in AngularJS is limited to arrays and does not function with objects due to the inherent unordered nature of JavaScript objects. To overcome this, developers can use custom filters to convert objects to arrays or implement sorting directly on objects. By understanding these concepts and applying the provided solutions, one can effectively sort data in ng-repeat regardless of the data structure.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.