Power Apps Sort Gallery [With 15 Useful Examples]

Do you know what is the Sort and Sort Gallery in Power Apps? In this Power Apps tutorial, we will read all the details about Power Apps Sort Gallery and its various examples.

Also, we will cover these below topics:

  • Power Apps sort gallery Or Power Apps gallery sort ascending descending
  • Power Apps sort gallery items
  • Power Apps sort gallery by choice column
  • Power Apps sort gallery by date
  • Power Apps sort gallery by label
  • Power Apps sort gallery based on dropdown
  • Power Apps sort gallery by id
  • Power Apps gallery sort and filter
  • Power Apps sort gallery by date and time
  • Power Apps sort and search gallery
  • Power Apps gallery sort by newest
  • Power Apps gallery sort distinct
  • Power Apps gallery sort get row number
  • Power Apps gallery sort by multiple columns
  • Power Apps sort gallery by calculated field
  • Power Apps sort gallery by checkbox

Power Apps Sort Function

  • Power Apps Sort is a type of function that helps to sort a table depending on the formula that is provided by the user. Moreover, another Power Apps function, i.e. SortByColumns, helps to sort the table based upon single or multiple columns.
  • We can sort the table or its items in Ascending or Descending order (user’s choice) in Power Apps.

Power Apps sort gallery or Power Apps gallery sort ascending descending

Whenever a user sorts the records in a Power Apps Gallery control, it is known as Power Apps Sort Gallery or Sort ascending descending in Power Apps Gallery. Check out the below simple scenario.

  • The below screenshot represents a SharePoint Document Library named Book Images. This library has all the default columns like Name, Modified, Modified By, etc. Also, it has some images as shown in the below screenshot.
PowerApps gallery sort ascending descending
PowerApps gallery sort ascending descending
  • In Power Apps, there is a Gallery control with a Text input control and a Sort icon. Now, what I would like to do is, that a user will search the book and the gallery will filter and display the search-related information.
  • Also, whenever a user will click on the sort icon, then the gallery filtered items will appear in ascending order, and vice versa when the user will again tap on the same sort icon.
Power Apps gallery sort ascending descending
Power Apps gallery sort ascending descending
  • To achieve this, select the Sort icon and apply the below code on its OnSelect property as:
OnSelect = UpdateContext({ctxSortDescending: !ctxSortDescending})

Where,

ctxSortDescending = Context variable name

Sort ascending descending in Power Apps gallery
Sort ascending descending in Power Apps gallery
  • Next, select the gallery control and set its Items property to the below formula:
Items = SortByColumns(
    Search(
        [@'Book Images'],
        txtSearchBook.Text,
        "{Name}"
    ),
    "{Name}",
    If(
        ctxSortDescending,
        Descending,
        Ascending
    )
)

Where,

  1. ‘Book Images’ = SharePoint Document Library Name
  2. txtSearchBook = Text input control name where a user can search the book name
  3. “{Name}” = This is the library column name
Sort ascending descending in PowerApps gallery control
Sort ascending descending in PowerApps gallery control
  • Save and preview the app. Once you will search any book inside the search box, you can see the gallery will filter and display those search-related records only. To sort (ascending or descending) the gallery items, you can tap on the sort icon as per your need.

This explains all about the sort ascending descending in Power Apps Gallery control.

Read: PowerApps Dropdown Gallery + Examples

Power Apps sort gallery items

Suppose in Power Apps, you want to sort the gallery items based upon any of the SharePoint fields (let’s say Title). Refer to the scenario below to do so.

  • There is a SharePoint list named Employee Onboarding and it has the fields below:
  1. Employee Name = By default, this is a Title column (with a Single line of text data type). To look better, I just renamed it to Employee Name.
  2. Employee Last Name = This is also a single line of text data type field
  3. Employee ID = It is a Number Data type where a user will enter the Employee ID value
  4. Department = This is a Choice column data type where a user will choose the department.
PowerApps sort gallery items
PowerApps sort gallery items
  • Next, in the app, there is a Gallery control that displays all the items from the SharePoint list. Now, I would like to sort (in ascending order) these items based on the SharePoint Title field.
  • To workaround this, select the gallery and apply the code below on its Items property as:
Items = SortByColumns(
    'Employee Onboarding',
    "Title",
    Ascending
)

Where,

  1. Employee Onboarding‘ = SharePoint list name
  2. Title” = Specify the SharePoint list field name that you want to sort. Instead of the Title, you can take any field that you want to sort in the gallery.
  3. Ascending = Mention the order either you want to view the field values in ascending or descending order.
Power Apps sort gallery items
Power Apps sort gallery items

This is how to work with sort gallery items in Power Apps.

Also, Read: PowerApps Search Function + How to use with example

Power Apps sort gallery by choice column

Next comes how we can sort the Power Apps Gallery control by the SharePoint Choice column. To achieve it easily, refer to the below simple scenario.

  • As you can see, there is a SharePoint list named Products. This list has different columns with different data types. Such as:
  1. ID = By default, this is an ID column (with Number data type) of the list.
  2. Title = By default, this is a Single line of text column.
  3. Vendor = It is a Choice field where a user can select the product vendor.
  4. Customer Name = This is a Single line of text field
  5. Quantity = Number field where a user can enter the product quantity
  6. Sales Date = This is a Date and time field
  7. Status = A Choice field where a user can view their product status like Submitted, Approved, Pending, Rejected, etc.
PowerApps sort gallery by choice column
PowerApps sort gallery by choice column
Items = SortByColumns(
    AddColumns(
        Products,
        "Status_value",
        Status.Value
    ),
    "Status_value",
    [
        "Submitted",
        "Pending"
    ]
)

Where,

  1. AddColumns = PowerApps AddColumns is a type of function that helps to add a column to a table. To read more details about AddColumns function, PowerApps AddColumns Function with Examples
  2. Products = SharePoint List name
  3. Status_value” = Provide a new unique column name
  4. Status = Specify the SharePoint Choice column that you want to sort. As it is a Choice field, that’s why we need to specify it with a .Value parameter.
  5. Submitted“, “Pending” = A column of values to be used for sorting purposes.
Power Apps sort gallery by choice field
Power Apps sort gallery by choice field
  • Finally, select the label from the gallery section and then set its Text property to the below code:
Text = ThisItem.Status_value

Status_value = Specify the new column name that we have added in the previous code

sort gallery by choice field PowerApps
sort gallery by choice field PowerApps

This is how to work with the sort gallery by choice column in Power Apps.

Checkout: Power Apps Search SharePoint List Examples

Power Apps sort gallery by date

Do you want to sort the Power Apps Gallery control by using the SharePoint Date field? If so, then how you will do so far? Check out the below scenario to achieve this.

  • In this scenario, I have taken the same above SharePoint list as Products and the same date field (Sales Date) is used to sort the gallery.
  • Select the gallery control and set its Items property to the below code:
Items = Sort(
    Products,
    DateValue('Sales Date'),
    Descending
)

Where,

DateValue = Power Apps DateValue is a type of function that helps to convert a date string to a date-time value. You can read this complete tutorial to know more details: How to use date time picker in PowerApps

Power Apps sort gallery by date
Power Apps sort gallery by date
  • When we will save and preview the app, the gallery will look like the image below (in descending order).

In this way, We can work with Power Apps to sort the gallery by date.

Power Apps sort gallery by label

Now comes how to sort the Power Apps gallery by a label or what formula we can use to achieve this. Overlook the scenario below to know more details about this.

  • In Power Apps, there is a Text input control and a Gallery control. A user will search the product over the search box and then the gallery will display all the search-related details in ascending order as shown below.
  • Let’s say, When I searched for a product like Laptop, the gallery displayed all the laptop information in alphabetical order.
Power Apps sort gallery by a label
Power Apps sort gallery by a label
  • To do so, we can use the code below on Gallery’s Items property as:
Items = SortByColumns(Filter(Products, StartsWith(Title, txtSearchProd.Text)), "Title", Ascending)

Where,

  1. Products = SharePoint List name
  2. Title = Specify the SharePoint field that the user will search in the search box
  3. txtSearchProd = Its the name of the text input control where a user will search the product
  4. Ascending = Specify the order that you would like to sort
Power Apps sort gallery by label
Power Apps sort gallery by label

This is how to work with Power Apps sort gallery by a label.

Read: Power Apps Search Gallery + 19 Examples

Power Apps sort gallery based on dropdown

Do you want to sort the gallery value based upon the Power Apps Dropdown control? Check out this post to know more details: Power Apps sort gallery based on dropdown

Power Apps sort gallery by id

Suppose a user wants to sort the Power Apps gallery control based upon the SharePoint ID column. In that case, he/she can refer to the scenario below.

  • The below screenshot represents a SharePoint list named Employee Onboarding. This list has these many below columns:
  1. ID = This is a default column with a Number data type.
  2. Employee Name = By default, this is a Title column (with a single line of text column). I just renamed it to Employee Name to look better.
  3. Employee Last Name = This is also a single line of text column.
  4. Employee ID = It is a Number data type field that contains the ID of the specific employee.
  5. Department = This is a Choice column that contains some choice values like HR, IT, FINANCE, etc.
  6. Location = This is also a Choice column that contains some choice values like Australia, America, France, Canada, Cuba, London, etc.

Now, I would like to sort the gallery based on the SharePoint ID column.

PowerApps sort gallery by id
PowerApps sort gallery by id
  • To work with it, the same and simple formula we can use it in the gallery’s Items property as:
Items = Sort(
    'Employee Onboarding',
    ID,
    Descending
)
  • Once we save and preview the app, the gallery will appear with the ID column in descending order as shown in the below figure.
Power Apps sort gallery by id
Power Apps sort gallery by id

This explains how to work with Power Apps sort gallery by id.

Also read: PowerApps Find Function With Examples

Power Apps gallery sort and filter

As we know, we can sort the gallery control based on any SharePoint field. Suppose you want to sort and filter the gallery at the same time. To work with it, we will use the Power Apps Filter and Sort function combination. Check out the scenarios below.

For all the below scenarios, I have used only one SharePoint list i.e. TSInfo Attachments. This list has many below columns with different data types:

  1. ID = By default, this is a Number column and it is a read-only field. A user cannot enter the ID value as it is an auto-generated field.
  2. Title = By default, this is a Single line of text data type.
  3. Attachment Types = This is a Choice column having some choice values like Microsoft Document, PPT, PDF, Excel, etc.
  4. Attachment Costs = This is a Currency field where a user can enter the Attachment cost.
  5. Attachment Created Date = It is a Date time column where a user can enter the created date of any attachment.
Sort and filter in PowerApps gallery
Sort and filter in PowerApps gallery

Now follow the below things.

Example – 1: (Filter by Attachment Types and Sort by Title field)

  • In this scenario, we will filter the Power Apps gallery by the SharePoint Attachment types field and sort by the Title field.
  • To achieve this, select the gallery control and set its Items property to the below formula:
Items = SortByColumns(
    Filter(
        [@'TSInfo Attachments'],
        'Attachment Types'.Value = "PDF"
    ),
    "Title",
    Ascending
)

Where,

  1. TSInfo Attachments = Specify the SharePoint list name
  2. ‘Attachment Types’ = Specify the column that you want to filter. As it is a Choice column, that’s why we need to specify a .Value parameter
  3. “PDF” = Specify the value that you want to filter from the specific column
  4. “Title” = Specify the column name that you want to sort in the gallery
  5. Ascending = Specify the sort order either ascending or descending
Power Apps gallery sort and filter
Power Apps gallery sort and filter

When we save and preview the app, we can see the filter and sort result in the gallery control as shown in the above screenshot.

Example – 2: (Filter by Attachment Types and Sort by Attachment Created Date field)

  • Next, we will filter the Power Apps gallery based on the Attachment types, and at the same time, the items will sort by the Attachment Created Date field.
  • To work with this, we can use the below code on the gallery’s Items property:
Items = Sort(
    Filter(
        'TSInfo Attachments',
        'Attachment Types'.Value = "Excel"
    ),
    'Attachment Created Date',
    Descending
)

Refer to the below screenshot.

PowerApps gallery sort and filter combination
PowerApps gallery sort and filter combination

Once we save and preview the app, we can see the gallery filtered with the choice value (PDF) and the date appeared in descending order.

Example – 3: (Filter by the first letter of Title and Sort by Attachment Created Date field)

Now in this scenario, we will see how to filter the first letter from the SharePoint Title field and sort the date field in Power Apps Gallery control.

  • To workaround this, we can refer to the below code on the gallery’s Items property:
Items = SortByColumns(
    Filter(
        [@'TSInfo Attachments'],
        StartsWith(
            Title,
            "P"
        )
    ),
    "AttachmentCreatedDate",
    Descending
)

Where,

  1. StartsWith = Power Apps StartsWith is a function that helps to check whether the text string begins with another. To know more details about this function, check out this complete post: PowerApps StartsWith and EndsWith Functions
  2. Title = Specify the SharePoint column that you want to filter
  3. P” = Mention the alphabet that starts within the title field
Power Apps gallery sort and filter combination
Power Apps gallery sort and filter combination
  • Save and Preview the app. The gallery will appear with the filtered value in descending order as shown in the above screenshot.

This is how to work with a combination of sort and a filter functions in Power Apps Gallery.

Also, read: PowerApps toggle control + How to use with examples

Power Apps sort gallery by date and time

As I already discussed above that how to work with Power Apps sort gallery by date. Similarly, to work and learn more about Power Apps sort the gallery by date and time, you can directly refer to this link: Power Apps Sort by Date and time

Power Apps sort and search gallery

Suppose we want to sort and search the Power Apps gallery based on any specific SharePoint field. Follow the below scenarios to get some more ideas.

  • For all the scenarios, I have taken one SharePoint list named Job Seekers Registration List. This list has the below columns with different data types:
  1. First Name = By default, it is a Title column with a Single line of text data type.
  2. SurName = Single line of text field
  3. Registration ID = This is a Number field that contains the ID value of a specific registrant.
  4. Matric Percentage, 12th Percentage, Graduation SGPA, Address for Correspondence = These are the single line of text fields
Sort and search PowerApps gallery
Sort and search PowerApps gallery

Refer to the below various examples.

Example – 1: (Power Apps sort and search gallery based on SharePoint Number field)

In this scenario, we will see how to sort and search the Power Apps Gallery based on the SharePoint Integer field i.e. Registration ID.

  • In the below screen, you can see whenever the user does not search any ID in the search box, the gallery appears with all list records with sorting the registration IDs.
  • When a user searches any specific ID in the search box (For example, 11), then the gallery filters and appears by searching related records with sorting.
Sort and search gallery in PowerApps
Sort and search gallery in PowerApps
  • To achieve this, we will use the AddColumns function to create a new column that will hold the integer value.
  • Select the Gallery control and apply the below code on its Items property as:
Items = SortByColumns(
    Search(
        AddColumns(
            'Job Seekers Registration List',
            "NewRegID",
            Text('Registration ID')
        ),
        txtSearchID.Text,
        "NewRegID"
    ),
    "RegistrationID"
)

Where,

  1. ‘Job Seekers Registration List’ = Specify the SharePoint List name
  2. “NewRegID” = Provide a unique name of the new field
  3. ‘Registration ID’ = Specify the SharePoint Number field. The Text function converts the integer value to a string.
  4. txtSearchID = This is the text input control name where a user will search the registration ID
Sort and search gallery in Power Apps
Sort and search gallery in Power Apps

Once we save and preview the app, the gallery will display all the user search-related records in ascending order.

Example – 2: (Power Apps sort and search gallery based on SharePoint Text field)

Next comes how we can sort and search the Power Apps Gallery based on the SharePoint Single line text field.

  • In the below screenshot, there is a Text input control and a Gallery control. When a user searches the Surname of any job seeker, then the specific surname filtered details will appear in the gallery control as shown below.
Power Apps sort and search gallery
Power Apps sort and search gallery
  • To do so, set the code below on the gallery’s Items property:
Items = SortByColumns(
    Search(
        'Job Seekers Registration List',
        txtSearchSurname.Text,
        "SurName"
    ),
    "SurName"
)

Where,

txtSearchSurname = Text input control name where a user can search the Surname

PowerApps sort and search gallery
PowerApps sort and search gallery
  • Save and Preview the app. When you will search for any Surname in the search box, then the gallery will filter and appear those search-related records.

Check out: PowerApps Now, Today, and IsToday function

Power Apps gallery sort by newest

Do you know what is the sort by newest in Power Apps Gallery? Getting the Gallery items from latest to oldest or vice versa is known as Power Apps Gallery sort by newest. Follow the below things to achieve it in Power Apps.

  • It’s quite similar to the Power Apps sort gallery by date topic where we are sorting the gallery items based on the date field. Similarly, to sort the gallery based on the newest items, we can use the Date column (either default Created Date field or any Custom Date field that you have created) in the app.
  • To workaround this, select the gallery control and set its Items property to the below code:
Items = Sort(
    'TSInfo Attachments',
    'Attachment Created Date',
    Descending
)

Where,

  1. ‘TSInfo Attachments’ = SharePoint List name
  2. ‘Attachment Created Date’ = Date time field
Power Apps gallery sort by newest
Power Apps gallery sort by newest
  • Now the items are sorted from new to old in the Power Apps Gallery control. Next, suppose, you want to include a Text search box without losing the Descending function within the gallery item. I would like the user to search items within a column named Title.
  • To work around this, write the below code on the gallery’s Items property as:
Items = SortByColumns(
    Filter(
        'TSInfo Attachments',
        txtSearchAttach.Text in Title
    ),
    "AttachmentCreatedDate",
    Descending
)

Where,

  1. txtSearchAttach = Text input control name where a user can search the Attachment
  2. Title = SharePoint single line of text field

Refer to the below screenshot.

PowerApps gallery sort by newest
PowerApps gallery sort by newest
  • Save and Preview the app. When you will search any attachment in the search box, the gallery will filter and sort based on the created date as shown in the above figure.

Power Apps gallery sort distinct

This topic will show how to sort the distinct values in Power Apps Gallery control. We will use the Power Apps Sort and Distinct function combination to achieve this.

  • For this thing, I have taken a SharePoint list named Products. This list has several different columns along with the Title column. I would like to filter all the distinct values from this title column and then sort them in the Power Apps Gallery control.
  • There are two types of formulas that we can try out:

Formula – 1:

  • Select the gallery control and set its Items property to the below code:
Items = Sort(
    Distinct(
        Products,
        Title
    ),
    Result
)

Where,

  1. Products = Mention the SharePoint List name
  2. Title = Specify the SharePoint column that you want to get the distinct values
  3. Result = All the distinct values will store in the Result column

Refer to the below screenshot.

Power Apps gallery sort distinct
Power Apps gallery sort distinct
  • Next, select the label control from the gallery and set its Text property as:
Text = ThisItem.Result
  • Once you will save and preview the app, the filtered sort records will appear in the gallery control as shown in the above screenshot.

Formula – 2:

There is another formula that you can try to work around this. Apply the below code on the gallery’s Items property as:

Items = Distinct(
    Sort(
        Filter(
            Products,
            true
        ).Title,
        Title,
        Ascending
    ),
    Title
)

Where,

Title = Specify the SharePoint column that you want to get the distinct values and sort them alphabetically

Sort distinct in Power Apps gallery
Sort distinct in Power Apps gallery
  • In the same way, select the label control from the gallery and set its Text property as:
Text = ThisItem.Result
  • Save and preview the app, the filtered sort records will appear in the gallery control.

This is how to work with Power Apps gallery sort distinct.

Power Apps gallery sort get row number

Do you want to sort and display the row number in the Power Apps Gallery control? If so, then I already mentioned all these things in the other post that you can refer it easily.

Checkout: Power Apps gallery sort get row number

Power Apps gallery sort by multiple columns

Well, also we can sort the Power Apps Gallery control by using multiple SharePoint fields or columns. Refer to the below things to know its syntax and uses.

Syntax:

The below code represents the syntax or formula to work with the Power Apps gallery sorted by multiple columns.

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

Where,

  1. Table = Specify the Data source name or table name
  2. ColumnName1 , ColumnName2, and so on = Provide the Data source column names that you want to sort
  3. SortOrder1, SortOrder2, and so on = Specify the sort order as Ascending or Descending as per the need

To view how it works with the Power Apps gallery, follow the below scenario.

Example:

  • I have a SharePoint list named Book Purchase Info. This list has four columns:
  1. Book Name = By default, this is a Single line of text Title column. I just renamed it to Book Name.
  2. Book Price = This is a Currency field having the book price value.
  3. Sales Date = It is a Date Time column.
  4. Book Seller = It is a Person field that contains the seller’s names.
PowerApps gallery sort by multiple columns
PowerApps gallery sort by multiple columns
  • Next, I would like to sort the gallery by two different SharePoint fields i.e. Book Name (Title) and Book Price.
  • To do so, select the gallery control and apply the below formula on its Items property as:
Items = SortByColumns(
    'Book Purchase Info',
    "Title",
    Ascending,
    "BookPrice",
    Descending
)

Where,

Ascending, Descending = Mention the order that you want to sort the gallery items

Refer to the below image.

Power Apps gallery sort by multiple columns
Power Apps gallery sort by multiple columns

Also, to read more details about Power Apps sort by multiple fields, checkout this forum post:

Sort gallery with multiple fields

This is how to work with the Power Apps gallery sort by multiple columns.

Power Apps sort gallery by calculated field

Do you want to sort the SharePoint Calculated column in a Power Apps Gallery control? refer to the below simple scenario.

  • There is a SharePoint list called COVID-19 Tracker. This list has five different data type fields. Such as:
  1. Country = This is a Choice column having some choice values like Australia, Brazil, Canada, Denmark, etc.
  2. Total Confirmed, Total Deaths, Total Recovered = These are the Number data type fields. And whenever you will create these columns, you must ensure to create the column names without giving any space.
  3. Deaths Percentage = This is a Calculated column that will calculate the Death percentage depending on the Total Recovered and Total Confirmed fields.
Power Apps sort gallery by calculated field
Power Apps sort gallery by calculated field
  • Now, I would like to sort the gallery control based upon the Calculated field i.e. Deaths Percentage. To do so, we will use the Power Apps AddColumns function.
  • Select the Power Apps gallery control and apply the below code on its Items property as:
Items = Sort(
    AddColumns(
        'COVID-19 Tracker',
        "SortField",
        TotalRecovered / TotalConfirmed * 100
    ),
    SortField,
    Descending
)

Where,

  1. SortField” = Provide a new unique name where all the percentage values will store.
  2. Descending = As I want to view the largest data first, that’s why I specified the order as descending.
PowerApps sort gallery by calculated field
PowerApps sort gallery by calculated field
  • Next, select the label from the gallery and set its Text property as:
Text = "Death Percentage: " & ThisItem.SortField
  • Once you will save and preview the app, the gallery will appear with sort filtered records based on the calculated field.

This is how to work with Power Apps sort gallery by calculated field.

Power Apps sort gallery by checkbox

Moreover, to work with Power Apps sort the gallery by checkbox, you can check out this forum post: Sort checked boxes to the top of Power Apps gallery

Also, you may like some below Power Apps Tutorials:

In this Power Apps tutorial, we discussed what is the difference between the Sort function and the Sort gallery in Power Apps. Also, by taking some simple scenarios, we covered these topics below:

  • Power Apps sort gallery Or Power Apps gallery sort ascending descending
  • Power Apps sort gallery items
  • Power Apps sort gallery by choice column
  • Power Apps sort gallery by date
  • Power Apps sort gallery by label
  • Power Apps sort gallery based on dropdown
  • Power Apps sort gallery by id
  • Power Apps gallery sort and filter
  • Power Apps sort gallery by date and time
  • Power Apps sort and search gallery
  • Power Apps gallery sort by newest
  • Power Apps gallery sort distinct
  • Power Apps gallery sort get row number
  • Power Apps gallery sort by multiple columns
  • Power Apps sort gallery by calculated field
  • Power Apps sort gallery by checkbox
>