Sorting is essential for any app’s good navigation, but most of the time, we want to define a column to be the “default” sorting column. SortByColumns Function helps us on that. We can define a column within a table, and that table is automatically sorted.

Important things to keep in mind:

  1. If your column names have spaces, like most Excel columns and SharePoint list columns, you have to include “x0020” instead of the space. For example, “My Awesome Column” should be ”My x0020 Awesome x0020 Column”.
  2. Delegation of columns. I’ll detail below the limitations, but keep in mind that some data sources won’t support delegation.

Usage

It follows a simple pattern.

  1. Table
  2. Column Name
  3. Sorting order(s). You can have multiple based on multiple columns.

Examples

Let’s look at a simple department list of people.

SortByColumns(Organization, "Department", SortOrder.Ascending)

Now let’s check this example:

SortByColumns(Organization, "Department_x0020_Responsible", SortOrder.Ascending)

Notice the “x0020”.

Final example. Let’s sat what you want to organize by Department and by name:

SortByColumns(Organization, "Department_x0020_Responsible".Ascending, "Name".Ascending)

You can add multiple sorting orders based on your data. There’s no difference in the case above, but you’ll see if you had multiple people in the same department.

Limitations

The biggest limitation is delegation. If you’re not aware of what delegation is, I explain it in detail here, but it’s when Power Apps can “delegate” to the data source the operation. For example, if the data source can sort the columns, Power Apps won’t need to do it since the data comes from the source already sorted. Here’s a reference of all delegable data sources and functions.

Recommendations:

  1. Don’t be scared of putting multiple fields. It will logically present your data to the user, and it won’t break delegation.
  2. Don’t nest it. There’s no real need to nest since you can add multiple sorting parameters. If you nest, the last “SortByColumns” will prevail since it will sort the first dataset and then sort again based on the last criteria. If you’re nesting “SortByColumns,” please double-check your logic because you’re probably doing something wrong.

Localization

  1. Please note that formulas may have localization-based differences. For example, you should write “aaa” with “separating each of the arguments, but if your regional settings are set to Portugal, you should use “;” instead.

Sources:

 Sort functions in Power Apps 

Back to the Power Apps Function Reference

Photo by Sophie Elvis on Unsplash

Manuel Gomes

I'm a Project Manager with experience in large projects and companies. I've worked in the past for companies like Bayer, Sybase (now SAP) and I'm currently working for Pestana Hotel Group.

View all posts by Manuel Gomes →

Leave a Reply

%d bloggers like this: