1. Home
  2. Languages
  3. AngularJS
  4. Mastering AngularJS: How to Sort Data Using the orderBy Filter

Mastering AngularJS: How to Sort Data Using the orderBy Filter


Sorting data dynamically is a common requirement in modern web applications. Whether it’s sorting lists, tables, or dropdowns, AngularJS offers a powerful and straightforward way to handle it via the orderBy filter. If you’re looking to implement sorting in your AngularJS project easily and efficiently, this guide will walk you through everything you need to know.


What is the AngularJS orderBy Filter?

The orderBy filter in AngularJS sorts an array by an expression, allowing you to arrange your data in ascending or descending order. It’s particularly useful when displaying data on the UI, such as sorting users by name, prices from low to high, or dates in reverse chronological order.


Why Use orderBy?

  • Simplicity: Easy to implement with minimal code.
  • Declarative: Sorting logic is cleanly written inside the view.
  • Dynamic Sorting: Change the sort criteria on the fly — perfect for interactive tables or lists.
  • Supports Multiple Sorts: Sort by one or more expressions.


Step-by-Step Guide to Implement orderBy

Step 1: Prepare Your AngularJS App and Data

Make sure you have AngularJS included in your project. Next, define the data array you want to sort.

javascript
var app = angular.module(‘sortApp’, []);

app.controller(‘MainCtrl’, function($scope) {
$scope.people = [
{ name: ‘John’, age: 25 },
{ name: ‘Mary’, age: 30 },
{ name: ‘Alex’, age: 22 },
{ name: ‘Kate’, age: 28 }
];

// Default sort expression
$scope.sortExpression = 'name';
// Reverse sort (descending)
$scope.reverseSort = false;

});

Step 2: Use orderBy in Your HTML Template

Apply the orderBy filter in your AngularJS template with the ng-repeat directive to display sorted data.





  • {{ person.name }} – {{ person.age }}

How This Works:

  • orderBy:sortExpression:reverseSort tells AngularJS to sort the people array by the key bound to sortExpression (either 'name' or 'age').
  • reverseSort determines if sorting is ascending (false) or descending (true).
  • Users can dynamically select the sorting field and toggle ascending/descending order.


Advanced Tips for orderBy Usage

  • Sorting by Multiple Expressions: Pass an array of expressions to sort by multiple fields.

    ng-repeat=”item in items | orderBy:[‘category’, ‘price’]”

  • Custom Sorting Functions: Use a function as the sorting expression for complex criteria.

  • Performance Considerations: For large datasets, sorting in AngularJS views can degrade performance. Consider sorting in the controller or server side if necessary.


Conclusion

Utilizing AngularJS’s orderBy filter is an effective way to enable sorting functionality in your application. It’s simple to add, user-friendly, and flexible enough for many use cases — perfect for enhancing UX with dynamic data presentation.

By following the steps above, you’ll provide your users with smooth, intuitive sorting on any dataset. Happy coding!


Keep your data organized and your users happy with easy sorting using AngularJS’s orderBy filter.

Updated on July 4, 2025
Was this article helpful?

Related Articles

Leave a Comment