Microsoft PowerApps: Sort and SortByColumns functions

Sort Function

The Sort function sorts a table based on a formula.

The formula is evaluated for each record of the table, and the results are used to sort the table. The formula must result in a number, a string, or a Boolean value; it can’t result in a table or a record.

Fields of the record currently being processed are available within the formula. Use the ThisRecord operator or simply reference fields by name as you would any other value. The As operator can also be used to name the record being processed which can help make your formula easier to understand and make nested records accessible. For more information, see the examples below and working with record scope.To sort first by one column and then by another, you embed a Sort formula within another. For example, you can use this formula to sort a Contacts table first by a LastName column and then by a FirstName column: Sort( Sort( Contacts, LastName ), FirstName )

 

Syntax :

SortTableFormula [, SortOrder ] )

  • Table – Required. Table to sort.
  • Formula – Required. This formula is evaluated for each record of the table, and the results are used to sort the table. You can reference columns within the table.
  • SortOrder – Optional. Specify SortOrder.Descending to sort the table in descending order. SortOrder.Ascending is the default value.

The example below , sorts IceCream by its Quantity column. The Quantity column contains numbers, so the sort is done numerically. The sort order has been specified as descending  as shown :

 

SortByColumns Function

The SortByColumns function can also be used to sort a table based on one or more columns.

The parameter list for SortByColumns provides the names of the columns to sort by and the sort direction per column. Sorting is performed in the order of the parameters (sorted first by the first column, then the second, and so on). Column names are specified as strings, requiring double quotes if directly included in the parameter list. For example, SortByColumns( CustomerTable, “LastName” ). You can combine SortByColumns with a Drop down or List box control to enable users to select which column to sort by.

In addition to sorting ascending or descending, SortByColumns can sort based on a single column table of values. For example, you can sort record based on the name of a day of the week by supplying [ “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday” ] as the sort order. All records which have Monday” will come first, followed by Tuesday, and so on. Records found that do not appear in the sort table are put at the end of the list.

 

Syntax :

SortByColumnsTableColumnName1 [, SortOrder1ColumnName2SortOrder2, … ] )

  • Table – Required. Table to sort.
  • ColumnName(s) – Required. The column names to sort on, as strings.
  • SortOrder(s) – Optional. SortOrder.Ascending or SortOrder.DescendingSortOrder.Ascending is the default. If multiple ColumnNames are supplied, all but the last column must include a SortOrder.

The example below , sorts IceCream by its Quantity column. The Quantity column contains numbers, so the sort is done numerically. The sort order has been specified as descending  as shown :

 

Both examples above do the same thing if put to use in the Microsoft PowerApps Environment and the result we would get would be like shown below :