18. How do you sort data using multiple columns in Power Apps?

In Power Apps, you can sort data using multiple columns in two ways: by using nested Sort functions or by using SortByColumns.

Method 1: Using Nested Sort (with Sort)

When using the Sort function, you must nest one Sort inside another to apply multiple sorting levels.

Example:

Sort(
    Sort(
        Students,
        Age,
        SortOrder.Descending
    ),
    Name,
    SortOrder.Ascending
)

How it works:

  • First, the Students table is sorted by Age.
  • Then, the result is sorted by Name.
  • Each Sort handles only one column at a time.

Method 2: Using SortByColumns (Recommended)

SortByColumns allows you to sort by multiple columns in a single function call.

Example:

SortByColumns(
    Students,
    "cr110_age",
    SortOrder.Descending,
    "cr110_name",
    SortOrder.Ascending
)
 

How it works:

  • The table is sorted first by Age.
  • Then it is sorted by Name.
  • No nesting is required.
  • The syntax is cleaner and easier to maintain.

Which Method Should You Use?

  • Use Sort if you need formula-based sorting.
  • Use SortByColumns when sorting directly by multiple columns.
  • For readability and cleaner code, SortByColumns is usually preferred.

Β Interview-Ready Answer

To sort data using multiple columns in Power Apps, you can either nest Sort functions or use SortByColumns with multiple column names. SortByColumns is cleaner and preferred for direct multi-column sorting.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top