How To Use Sort And SortByColumn Functions In Microsoft PowerApps

Before reading this article, please go through the article links given below.

In PowerApps, we can use the Sort and SortByColumn functions.

Sort Function

The sort function sorts the data in the table. The formula is evaluated for each record of the table and sorts 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.

Syntax

Sort(Table, Formula [, SortOrder])

Explanation

  • Sort Keyword
  • Table Table Name
  • The formula We apply the formula(If you search more than one column)
  • SortOrder Either Ascending(SortOrder.Ascending) /Descending(SortOrder.Descending)

Follow the below steps to work with the Sort function in PowerApps.

Step 1. Log into the PowerApps.

After downloading the PowerApps from the Windows Store, here, we need Microsoft-related organizations (MSDN, Microsoft, Skype, Office 365, etc.,) login ID to log into it.

Powerapp

Step 2. Create a new app in PowerApp.

After login, we will see the Dashboard. There, click on the New button.

Dashboard

Step 3. Choose the Blank App.

Blank App

Step 4. Designing the app.

Now, let's start designing the app. On the left side, we can see the Individual screens for adding our data, while on the right side, we see the List of Layouts. The formula bar is shown at the top. There, the selected properties are shown. On the right side, we see the "Add Data Source" to add the external data source.

Layouts

Step 5. Choose the Blank Layout.

 Blank Layout

Step 6. Insert the Button from the Insert Menu.

Insert Menu

Step 7. Add the coding.

Click on the button and add the following code to the OnSelect Event.

Coding

ClearCollect(SivaEnterprise,
    {
        ProductNo: 100,
        ProductName: "Keyboard",
        Rate: 500
    },
    {
        ProductNo: 579,
        ProductName: "Mouse",
        Rate: 600
    },
    {
        ProductNo: 112,
        ProductName: "DVD",
        Rate: 1500
    },
    {
        ProductNo: 856,
        ProductName: "Modem",
        Rate: 500
    },
    {
        ProductNo: 469,
        ProductName: "Processor",
        Rate: 5000
    }
)

Onselect event

Step 8. Run the App.

Run app

Step 9. Click on the Display button and close the Preview window.

Display button

Step 10. Display the data.

Now, go to the File menu and choose Collections.

File menu

Collection

It will display the content in the table format.

Examples of Sorting functions

Note. To run every example, create a new button paste the example coding to the OnSelecct event, and follow Steps 7 to 10.

Example 1

ClearCollect(SivaEnterprise, Sort(SivaEnterprise, ProductName))
  • Explanation It will sort the ProductName by Ascending order (By default it's Ascending order)
  • ClearCollect It deletes all the records from a collection and then adds a different set of records to the same collection.
  • SivaEnterprise It’s the Collection name
  • Sort-Keyword.
  • SivaEnterprise Collection name.
  • ProductName Column name to sort.

Output

Output

Example 2

ClearCollect(SivaEnterprise, Sort(SivaEnterprise, ProductName, SortOrder.Descending))
  • Explanation: It will sort the ProductName by descending order.
  • SortOrder.Descending: Sort in Descending order.

Output

ProductName

SortByColumns Function

The SortByColumns function can also be used to sort a table based on one or more columns. You can combine SortByColumns with a Dropbox for List box control to enable users to select \which column to sort by.

Syntax

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

Explanation

  • SortByColumn Keyword
  • Table Table Name
  • ColumnName1, ColumnName2 more than one column we added
  • SortOrder Either Ascending(SortOrder.Ascending) /Descending(SortOrder.Descending)

Examples of SortByCoulumn Function

Example 1

ClearCollect(
    SivaEnterprise,
    SortByColumns(
        SivaEnterprise,
        "ProductName",
        ["DVD", "Mouse"]
    )
)

Explanation It will display the DVD and Mouse as first and second data.

Output

DVD and Mouse

Tables are a value in PowerApps, 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.

Conclusion

I hope you understand how to use Sort and SortByColumn in Microsoft PowerApps and how to run it.


Similar Articles