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

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, or a string, or a Boolean value; it can't result in a table or a record.

Syntax

  1. Sort (Table, Formula [, SortOrder ] )  
Explanation
  • Sort Keyword
  • Table Table Name
  • Formula We apply formula(If you search more than one column)
  • SortOrder Either Ascending(SortOrder.Ascending) /Decending(SortOrder.Descending)

Follow the below steps to work with 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.



Step 2 - Create a new app in PowerApp

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



Step 3 - Choose the Blank App



Step 4 - Designing the app

Now, let's start designing the app. In 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.



Step 5 - Choose the Blank Layout



Step 6 - Insert the Button from Insert Menu



Step 7 - Add the coding

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

Coding

  1. ClearCollect(SivaEnterprise,   
  2. {  
  3.     ProductNo: 100,  
  4.     ProductName: "Keyboard",  
  5.     Rate: 500  
  6. }, {  
  7.     ProductNo: 579,  
  8.     ProductName: "Mouse",  
  9.     Rate: 600  
  10. }, {  
  11.     ProductNo: 112,  
  12.     ProductName: "DVD",  
  13.     Rate: 1500  
  14. }, {  
  15.     ProductNo: 856,  
  16.     ProductName: "Modem",  
  17.     Rate: 500  
  18. }, {  
  19.     ProductNo: 469,  
  20.     ProductName: "Processor",  
  21.     Rate: 5000  
  22. })  


Step 8 - Run the App



Step 9

Click on the Display button and close the Preview window.



Step 10 - Display the data

Now, go to the File menu and choose the Collections.





It will display the content in the table format.

Examples for Sorting function:

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

Example 1
  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



Example 2

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

Output



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 Drop box for List box control to enable users to select \which column to sort by.

Syntax

  1. SortByColumns( Table, ColumnName1 [, SortOrder1, ColumnName2, SortOrder2, ... ] )  
Explanation 
  • SortByColumn Keyword
  • Table Table Name
  • ColumnName1, ColumnName2 more than one coulmns we added
  • SortOrder Either Ascending(SortOrder.Ascending) /Decending(SortOrder.Descending)

Examples for SortByCoulumn Function.

Example 1

  1. ClearCollect( SivaEnterprise, SortByColumns( SivaEnterprise, "ProductName", ["DVD""Mouse"]) )  
Explanation It will display the DVD and Mouse as first and second data.

Output



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 understood how to use Sort and SortByColumn in Microsoft PowerApps and how to run it.