cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Power Automate
Power Automate

Sort Ascending / Decending

What property drives which colum will be sorted when tapping the sort ascending/decending icon? Mine seems to be doing it's own thing. Smiley Frustrated

 

sort.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Power Apps
Power Apps

Re: Sort Ascending / Decending

If you're sorting items in a gallery, you might need to update the Items property of that gallery.

 

Here's an example of the default formula that's built when you generate an app automatically based on a data source (named Contact):

 

SortByColumns(Search(Contact, TextSearchBox1.Text, "BusinessPhone","CellPhone","ContactId","CreatedByUser"), "BusinessPhone", If(SortDescending1, Descending, Ascending))

 

The items are sorted by the column I've emphasized in bold. In many/most cases, you'll want to update the formula so that the items are sorted by a different column, for example, LastName.

 

SortByColumns(Search(Contact, TextSearchBox1.Text, "BusinessPhone","CellPhone","ContactId","CreatedByUser"), "LastName", If(SortDescending1, Descending, Ascending))

View solution in original post

28 REPLIES 28
Highlighted
Power Apps
Power Apps

Re: Sort Ascending / Decending

If you're sorting items in a gallery, you might need to update the Items property of that gallery.

 

Here's an example of the default formula that's built when you generate an app automatically based on a data source (named Contact):

 

SortByColumns(Search(Contact, TextSearchBox1.Text, "BusinessPhone","CellPhone","ContactId","CreatedByUser"), "BusinessPhone", If(SortDescending1, Descending, Ascending))

 

The items are sorted by the column I've emphasized in bold. In many/most cases, you'll want to update the formula so that the items are sorted by a different column, for example, LastName.

 

SortByColumns(Search(Contact, TextSearchBox1.Text, "BusinessPhone","CellPhone","ContactId","CreatedByUser"), "LastName", If(SortDescending1, Descending, Ascending))

View solution in original post

Highlighted
Power Automate
Power Automate

Re: Sort Ascending / Decending

I'm wondering if we are referring to the same thing? Here is what is there out of box for my app: There is way less than what you have in yours.

 

Do I leave that the same and change the sort for the browing gallery?

 

oob.PNG

Highlighted
Power Automate
Power Automate

Re: Sort Ascending / Decending

That worked!  (I am going to put an idea in that they made the change behind the scenes as the gallery items are selected.)

 

Thank you!

Highlighted
Helper I
Helper I

Re: Sort Ascending / Decending

Hello, I'm having a similar issue to the one above, I can't seem to sort my table in more than one way. The table with its defult sort only sorts by the name of the items, I'd like there to be more ways to sort the table than just the name, such as by a value or a sub-name. Is this possible and how? From the picture below im showing the two buttons, the highlighted button has the above code, the button with a black arrow going towards it is a button that I want to sort my table by a different value with.

Can I make the other button sort a different value?

Highlighted
Power Participant
Power Participant

Re: Sort Ascending / Decending

Can you please copy-paste the Items property of your gallery? Then I can suggest how to edit it.

You also need to change the SortDescending1 of one of your buttons' OnSelect property to something different like SortDescending2.

Highlighted
Helper I
Helper I

Re: Sort Ascending / Decending

I will do my best to explain what I want from these buttons with an example:

 

The items in my list have the property NAME, and also the property ATK which is a numerical value.

The first button was put in by powerapps, and was not made by me, its OnSelect Property is as follows:

 

UpdateContext({SortDescending1: !SortDescending1})

 

When the button is clicked, the items are arranged in alphabetic order, and then when clicked again they are ordered in reverse alphabetical order. I am not sure if PowerApps is doing this based on the NAME value of each item however, as the google sheet that these items are from is already ordered alphabeticlly.

 

I would like to order the items by things other than their NAME, for example, by their ATK, which is a numerical value. These are two of the items that exist in my sheet:

NAME: Alexandrite Dragon
ATK: 2000

NAME: BLUE-EYES WHITE DRAGON
ATK: 3000

 

If these buttons were to work correctly, when ordered alphabetically Alexandrite should be first, but by ATK, Blue-eyes should come first. 
I have made my second button say:

UpdateContext({SortDescending2: !SortDescending2})

 

However this does nothing, probably because I have not told the program what to do when sorting "2", and I have no idea where to find this or how to do this. If you know where these sort methods are being made and how they work, I would greatly appreciate being told where to find them in powerapps, my design is to have my list of items sorted by many items, so there wil be multiple items all being sorted by different values they have.

Highlighted
Power Participant
Power Participant

Re: Sort Ascending / Decending

Hi Huseyin, PowerApps does not work that way.  There is no Sort action or command that you can associate to a button.

 

This is how it works:

1) The gallery in which the list of your items is shown has an Items property, which determines what it shows

2) Typically this property would be something like SortByColumns(MyDataSource, "NAME", If(SortDescending1, Descending, Ascending)).  This means that the items are sorted in alphabetical order if the context variable SortDescending1 is True or sorted in reverse alphabetical order if SortDescending1 is False

3) When you press the original button, all it does is change the value of SortDescending1 from True to False or viceversa, and this is enough for the items to be resorted

 

Hence if you now want to sort by two columns, you need to clarify how you want it to work and be creative.  For example the second button could toggle the value of SortDescending2 (which is what I think you have done already), but that is not enough because you also need to determine which priority to sort the fields, hence I would suggest that the OnSelect property of your two buttons should be:

 

UpdateContext({SortDescending1: !SortDescending1, SortPriority: "NAME"})
UpdateContext({SortDescending2: !SortDescending2, SortPriority: "ATK"})

Then you should set the Items property of your gallery to be something like

 

If(SortPriority="NAME",
     SortByColumns(MyDataSource, "NAME", If(SortDescending1, Descending, Ascending)),
     If(SortPriority="ATK",
          SortByColumns(MyDataSource, "ATK", If(SortDescending2, Descending, Ascending)),
          MyDataSource))

Please let me know if still not clear, or if you need help in finding the Items property of the gallery

Highlighted
Helper I
Helper I

Re: Sort Ascending / Decending

Thank you very much Steelman!

In the end I managed to get the Item Property of the gallery looking something like this:

If(SortPriority="NAME", SortByColumns(Search('MyDataSource',  "NAME", If(SortDescending1, Descending, Ascending)), If(SortPriority="ATK", SortByColumns(Search('MyDataSource',  "ATK", If(SortDescending2, Descending , Ascending))))

 

With the onSelect for the buttons already working correctly.

You have been a great help and I'm very thankful that you got back to me so quickly whenever I posted here.

There is a small issue with the ATK items only being sorted after the first couple of items, for example:

500
1900
100
0
3000
2950
2900
....

 

This is just an example to show you what I mean, things only descend properly after the first couple of items for some reason,however this may simply be something on my end and I'll hopefully resolve it without the need for a reply.

Thanks again!

Highlighted
Helper I
Helper I

Re: Sort Ascending / Decending

As one small follow up, I've tried sorting by a third value, DEF, however whenever I try to add this as a third If condition, nothing seems to work, are conditions locked to 2 max or is there some sort of workaround? 

Helpful resources

Announcements
August 2020 Community Challenge: Can You Solve These?

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

secondImage

Return to Workplace

Reopen responsibly, monitor intelligently, and protect continuously with solutions for a safer work environment.

secondImage

Super Users Coming in August

We are excited for the next Super User season.

secondImage

Community User Group Member Badges

FIll out a quick form to claim your community user group member badge today!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (6,635)