Sort and SortByColumns functions in Power Apps - Power Platform (2023)

  • Article
  • 7 minutes to read

Sorts a table.

Description

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 )

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" ).

(Video) Sorting in PowerApps using Sort Icon

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.

Tables are a value in Power Apps, just like a string or number. They can be passed to and returned from functions. Sort and SortByColumn don't modify a table; instead they take a table as an argument and return a new table that has been sorted. See working with tables for more details.

Delegation

When possible, Power Apps will delegate filter and sort operations to the data source and page through the results on demand. For example, when you start an app that shows a Gallery control filled with data, only the first set of records will be initially brought to the device. As the user scrolls, additional data is brought down from the data source. The result is a faster start time for the app and access to very large data sets.

However, delegation may not always be possible. Data sources vary on what functions and operators they support with delegation. If complete delegation of a formula isn't possible, the authoring environment will flag the portion that can't be delegated with a warning. When possible, consider changing the formula to avoid functions and operators that can't be delegated. The delegation list details which data sources and operations can be delegated.

If delegation is not possible, Power Apps will pull down only a small set of records to work on locally. Filter and sort functions will operate on a reduced set of records. What is available in the Gallery may not be the complete story, which could be confusing to users.

See the delegation overview for more information.

Syntax

Sort( Table, Formula [, 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.

SortByColumns( Table, ColumnName1 [, SortOrder1, ColumnName2, SortOrder2, ... ] )

(Video) Custom sorting in PowerApps | Sort on Choice column | PowerApps | Power Platform for Beginners

  • Table - Required. Table to sort.

  • ColumnName(s) - Required. The column names to sort on, as strings.

  • SortOrder(s) - Optional. SortOrder.Ascending or SortOrder.Descending. SortOrder.Ascending is the default. If multiple ColumnNames are supplied, all but the last column must include a SortOrder.

    Note

    For SharePoint and Excel data sources that contain column names with spaces, specify each space as "_x0020_". For example, specify "Column Name" as "Column_x0020_Name".

SortByColumns( Table, ColumnName, SortOrderTable )

  • Table - Required. Table to sort.

  • ColumnName - Required. The column name to sort on, as strings.

    (Video) PowerApps Tuesday Tutorials #25 SortByColumns

  • SortOrderTable - Required. Single column table of values to sort by.

    Note

    For SharePoint and Excel data sources that contain column names with spaces, specify each space as "_x0020_". For example, specify "Column Name" as "Column_x0020_Name".

Examples

For the following examples, we'll use the IceCream data source, which contains the data in this table:

Sort and SortByColumns functions in Power Apps - Power Platform (1)

FormulaDescriptionResult
Sort( IceCream, Flavor )

SortByColumns( IceCream, "Flavor" )

Sorts IceCream by its Flavor column. The Flavor column contains strings, so the table is sorted alphabetically. By default, the sort order is ascending.Sort and SortByColumns functions in Power Apps - Power Platform (2)
Sort( IceCream, Quantity )

SortByColumns( IceCream, "Quantity" )

Sorts IceCream by its Quantity column. The Quantity column contains numbers, so the table is sorted numerically. By default, the sort order is ascending.Sort and SortByColumns functions in Power Apps - Power Platform (3)
Sort( IceCream, Quantity, SortOrder.Descending )

SortByColumns( IceCream, "Quantity", SortOrder.Descending )

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.Sort and SortByColumns functions in Power Apps - Power Platform (4)
Sort( IceCream, Quantity + OnOrder )Sorts IceCream by the sum of its Quantity and OnOrder columns for each record individually. The sum is a number, so the table is sorted numerically. By default, the sort order is ascending. Since we are sorting by a formula and not by raw column values, there is no equivalent using SortByColumns.Sort and SortByColumns functions in Power Apps - Power Platform (5)
Sort( Sort( IceCream, OnOrder ), Quantity )

SortByColumns( IceCream, "OnOrder", Ascending, "Quantity", Ascending )

Sorts IceCream first by its OnOrder column, and then by its Quantity column. Note that "Pistachio" rose above "Vanilla" in the first sort based on OnOrder, and then together they moved to their appropriate place based on Quantity.Sort and SortByColumns functions in Power Apps - Power Platform (6)
SortByColumns( IceCream, "Flavor", ["Pistachio","Strawberry"] )Sorts IceCream by it's Flavor column based on the single column table containing "Pistachio" and "Strawberry". Records which have a Flavor of "Pistachio" will appear first in the result, followed by records that contain "Strawberry". For values in the Flavor column that are not matched, such as "Vanilla", they will appear after the items that were matched.Sort and SortByColumns functions in Power Apps - Power Platform (7)

Step by step

To run these examples yourself, create the IceCream data source as a collection:

  1. Add a button, and set its OnSelect property to this formula:
    ClearCollect( IceCream, { Flavor: "Chocolate", Quantity: 100, OnOrder: 150 }, { Flavor: "Vanilla", Quantity: 200, OnOrder: 20 }, { Flavor: "Strawberry", Quantity: 300, OnOrder: 0 }, { Flavor: "Mint Chocolate", Quantity: 60, OnOrder: 100 }, { Flavor: "Pistachio", Quantity: 200, OnOrder: 10 } )
  2. Preview the app, select the button, and then press Esc to return to the default workspace.
  3. Select Collections on the File menu to display the collection that you just created, and then press Esc to return to the default workspace.

Sort

  1. Add another button, and set its OnSelect property to this formula:
    ClearCollect( SortByFlavor, Sort( IceCream, Flavor ) )

    The previous formula creates a second collection, named SortByFlavor, that contains the same data as Ice Cream. However, the new collection contains the data sorted alphabetically by the Flavor column in ascending order.

    (Video) Power Apps: Sorting

  2. Press F5, select the new button, and then press Esc.

  3. Select Collections on the File menu to display both collections, and then press Esc to return to the default workspace.

  4. Repeat the last three steps, but change the name of the collection that you want to create, and replace the Sort formula with a different formula from the table of examples earlier in this section that uses Sort.

SortByColumns

  1. Add another button, and set its OnSelect property to this formula:
    ClearCollect( SortByQuantity, SortByColumns( IceCream, "Quantity", Ascending, "Flavor", Descending ) )

    The previous formula creates a third collection, named SortByQuantity, that contains the same data as Ice Cream. However, the new collection contains the data sorted numerically by the Quantity column in ascending order, and then by the Flavor column in descending order.

  2. Press F5, select the new button, and then press Esc.

  3. Select Collections on the File menu to display all three collections, and then press Esc to return to the default workspace.

  4. Repeat the last three steps, but change the name of the collection that you want to create, and replace the SortByColumns formula with a different formula from the table of examples earlier in this section that uses SortByColumns.

FAQs

What does the sort () function do? ›

SORT returns a sorted array of the elements in an array. The returned array is the same shape as the provided array argument.

What is the use of sort () function? ›

Sorts the rows of a given array or range by the values in one or more columns.

What is the easiest way to reorder columns in power query? ›

You can also find this option when you right-click a column. If you want to move one column to the left, then select Before. The new location of the column is now one column to the left of its original location. If you want to move one column to the right, then select After.

Which can be used for sorting on multiple columns? ›

Custom Sort - sorts data in multiple columns by applying different sort criteria.

How do you handle a ComboBox? ›

The ComboBox control is used to display a drop-down list of various items. It is a combination of a text box in which the user enters an item and a drop-down list from which the user selects an item. Let's create a combo box by dragging a ComboBox control from the Toolbox and dropping it on the form.

What is the difference between sort () and sorted () functions? ›

The sort() function returns nothing and changes the original sequence, while the sorted() function creates a new sequence type containing a sorted version of the given sequence.

What is sort () and give example? ›

Sorting is the process of placing elements from a collection in some kind of order. For example, a list of words could be sorted alphabetically or by length. A list of cities could be sorted by population, by area, or by zip code.

Which is a benefit of sort? ›

Benefits Sort. Empathy and field dependent thinking. fosters a bigger picture point of view. "Putting ourselves in someone else's shoes—a part of our subconscious behavior—causes measurable changes in our cognitive style, increasing our so-called field-dependent thinking.

Which sorting is used in sort function? ›

Sort Algorithm

Internally it uses IntroSort, which is a combination of QuickSort, HeapSort and InsertionSort.

What algorithm does sort () use? ›

Python's default sort uses Tim Sort, which is a combination of both merge sort and insertion sort. Implementation of that will be covered in another article.

Is sort () or sorted () faster? ›

sort is slightly faster than sorted and consumes around 24% less memory. However, keep in mind that list. sort is only implemented for lists, whereas sorted accepts any iterable.

Can you query columns in any order? ›

The order doesn't matter, actually, so you are free to order them however you'd like. edit: I guess a bit more background is helpful: As far as I know, the process of optimizing any query happens prior to determining exactly what subset of the row data is being pulled.

Can you sort multiple columns in Power Query? ›

You can sort a table in Power Query by one column or multiple columns.

How do you sort columns without messing up formulas? ›

To sort linked data and keep formulas without changed, you can change the references in formulas to an absolute reference, then sorting the data. Therefore the data will keep the formulas even if their orders change.

How do I enable grid sorting? ›

To enable sorting, set the AllowSorting property to true . When sorting is enabled, the heading text for each column field with its SortExpression property set is displayed as a link button. The SortExpression property for an automatically generated columns field is automatically populated.

How do I filter a list in PowerApps? ›

You can use text input control in a canvas app to input text and filter the list such as a data table to filter list items from the connected list. To search using text input and to filter the records, you have to use the function filter. For example, Filter([@Colors], StartsWith(Title, TextInput1.

What are the two basic types of sorting? ›

The techniques of sorting can be divided into two categories. These are: Internal Sorting. External Sorting.

What are the two different methods for sorting? ›

Sorting is the processing of arranging the data in ascending and descending order. There are several types of sorting in data structures namely – bubble sort, insertion sort, selection sort, bucket sort, heap sort, quick sort, radix sort etc.

How do I Sort columns independently? ›

If you want to sort the table columns independently from each other, click on the Arrange All button in the ribbon toolbar tab Variables. After clicking, the Arrange_All function appears in the sidebar.

Can we use 2 columns in ORDER BY clause? ›

You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.

Can we sort data in multiple fields? ›

Click Ascending or Descending to choose the sort order. Click the Then sort by arrow, click the next field, then choose a sort order. Click up to two more fields and their sort orders. Choose (none) if you don't want to sort the fields at that level.

What are the three ComboBox styles? ›

Three types of combo boxes are: standard, drop-down (the default), and drop-down list. Set the Style property to select the type of combo box you need: Use csDropDown to create an edit box with a drop-down list.

How do I sort a Groupby output? ›

Sort within Groups of groupby() Result in DataFrame

By using DataFrame. sort_values() , you can sort DataFrame in ascending or descending order, before you use this first group the DataFrame rows by using DataFrame. groupby() method. Note that groupby preserves the order of rows within each group.

How do you handle more than 2000 records in PowerApps? ›

4) Collecting records in PowerApps

But how do we collect more than 2000 records in a single collection? The answer is you need to collect data in different collections and than merge all your collections into a single collection.

How do I get more than 5000 rows from a SharePoint list into a Powerapp? ›

If you genuinely need to process more than 5000 items at a time, you'd want to call the SharePoint batch API to speed things up and even then need to create a loop with the do until action as a batch call can't have more than 1000 operations.

How can I improve my PowerApps performance? ›

The more controls you add, the more generation time Power Apps needs. You can, in some cases, achieve the same result and have the app start faster if you use a gallery instead of individual controls. In addition, you might want to reduce the number of control types on the same screen.

What happens when a user clicks on ComboBox? ›

When the user clicks the mouse button, the hyperlink is activated, and then the Click event occurs. Selects an item in a combo box or list box, either by pressing the arrow keys and then pressing the Enter key or by clicking the mouse button.

What is the difference between combo box and list box? ›

Generally, a combo box is appropriate when there is a list of suggested choices, and a list box is appropriate when you want to limit input to what is on the list. A combo box contains a text box field, so choices not on the list can be typed in.

Does ComboBox allow multiple selections? ›

The combobox enables the user to indicate the selected item from the datasource when selecting multiple items from the dropdown.

How do you use the sort and filter feature? ›

To focus on a specific set of your data, you can filter a range of cells or a table. Click any cell in the range or table. On the HOME tab, click Sort & Filter, and click Filter. Click a drop-down arrow at the top of one of the columns to display its filter options.

How will you apply the sort command? ›

SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically. SORT command sorts the contents of a text file, line by line.

What are the different types of filters? ›

Four Major Types of Filters

The four primary types of filters include the low-pass filter, the high-pass filter, the band-pass filter, and the notch filter (or the band-reject or band-stop filter).

What is the difference between sort and filter feature? ›

You sort data to quickly organize your data and to find the data that you want. You filter data to display only the rows that meet criteria that you specify and hide rows that you do not want displayed, for one or more columns of data.

What is the difference between data sorting and data filtering? ›

Essentially, sorting and filtering are tools that let you organize your data. When you sort data, you are putting it in order. Filtering data lets you hide unimportant data and focus only on the data you're interested in.

What is the process for sort? ›

Sorting is the process of arranging data into meaningful order so that you can analyze it more effectively. For example, you might want to order sales data by calendar month so that you can produce a graph of sales performance. You can use Discoverer to sort data as follows: sort text data into alphabetical order.

Why is sort () not working? ›

If it looks like the data did not sort properly, refer to the following list of possible solutions: Make sure no hidden rows or columns exist. Use a single row for headers. If you need a multiline header, either wrap the text in the cell or use Alt+Enter to force line breaks in the cell.

Is sort () a transformation? ›

The Sort transformation sorts input data in ascending or descending order and copies the sorted data to the transformation output. You can apply multiple sorts to an input; each sort is identified by a numeral that determines the sort order.

What is the easiest way to reorder columns in Power Query? ›

You can also find this option when you right-click a column. If you want to move one column to the left, then select Before. The new location of the column is now one column to the left of its original location. If you want to move one column to the right, then select After.

How do I sort multiple columns in a query? ›

Syntax: SELECT * FROM table_name ORDER BY column_name; For Multiple column order, add the name of the column by which you'd like to sort records first. The column that is entered at first place will get sorted first and likewise.

Videos

1. Power Apps Sorting - Multi Column Sort, Sort Button, Sort and Filter
(April Dunnam)
2. Lets Make Sorting EASY in Power Apps with no Delegation Errors
(Andrew Hess - MySPQuestions)
3. Sort and Filter by multiple columns (People Picker) in Power Apps
(Power your SharePoint)
4. Difference between Sort and SortByColumns in PowerApps #shorts 54
(Mayuresh Joshi (office365notes.com))
5. Create a sort button in PowerApps
(Shane Young)
6. DropColumns vs ShowColumns in Power Apps
(Cloudatica)
Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated: 03/04/2023

Views: 6406

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.