orderBy multiple fields in Angular

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for orderBy multiple fields in Angular

How to OrderBy Multiple Fields in Angular

Sorting data in Angular is a common operation, but what if you need to sort by multiple fields at the same time? For example, what if you have a list of divisions and you want to sort them first by group and then by sub-group? In this blog post, I'll show you a simple solution to this problem using the orderBy filter in Angular.

The Problem

Let's consider the following example, where we have an array of divisions:

$scope.divisions = [{'group':1,'sub':1}, {'group':2,'sub':10}, {'group':1,'sub':2}, {'group':1,'sub':20}, {'group':2,'sub':1},
    {'group':2,'sub':11}];

We want to display these divisions in the following format:

group : sub-group
1 - 1
1 - 2
1 - 20
2 - 1
2 - 10
2 - 11

The Solution

To achieve the desired sorting, we can use the orderBy filter in Angular. The orderBy filter allows us to sort an array based on the values of one or more fields.

To sort the divisions array by group and then by sub-group, we can use the following code:

<select ng-model="divs" ng-options="(d.group+' - '+d.sub) for d in divisions | orderBy:['group', 'sub']" />

In this code, we use the orderBy filter with an array of fields to sort by. The divisions array is then passed to the ng-options directive to populate the dropdown list. The result is a sorted list of divisions, first by group and then by sub-group, displayed in the desired format.

Conclusion

Sorting data by multiple fields in Angular doesn't have to be complicated. By using the orderBy filter and specifying the fields to sort by, you can easily achieve the desired sorting order. Next time you need to sort data in your Angular application, remember this simple solution.

Share this blog post with your fellow Angular developers to help them solve the problem of ordering by multiple fields. And don't forget to leave a comment below if you have any questions or other Angular topics you'd like us to cover. Happy sorting! 😄🔀

Sources:

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my