cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
TorbenSBendixen
Frequent Visitor

Sort If options in different ways

I have this code:

If(
SorterOptions.SelectedText.Value = "Title", Title,
SorterOptions.SelectedText.Value = "Status", Status.Value,
SorterOptions.SelectedText.Value = "Created", Value(Created)
)

I would like to have the last line sorted Descending and the others sorted Ascending, but don't know how to code it 🙂

 

Help is much appreciated, thank you!

5 REPLIES 5
GarethPrisk
Super User II
Super User II

You won't be able to dynamically select the Sort or SortByColumns without hitting a delegation warning.

 

However, you can switch and execute different Sort/SortByColumns calls. For example, you would switch on your SorterOptions dropdown and then have three separate data calls. This feels redundant, because it is, but is the best way to accomplish this.

 

Switch(
    SorterOptions.SelectedText.Value,
    // First scenario to filter/sort
    "Title",
    Sort(
        YourDataSource,
        "Title",
        Ascending
    ),
    // Second scenario to filter/sort
    "Status",
    Sort(
        YourDataSource,
        Status.Value, // Need to check this, may not be possible
        Ascending
    ),
    // Third scenario to filter/sort
    "Created",
    Sort(
        YourDataSource,
        Value(Created),
        Descending
    ),
    // Default
     SortByColumns(
        YourDataSource,
        "Title", // Define this
        Ascending
    )
)

 

RandyHayes
Super User III
Super User III

@TorbenSBendixen 

If you want static sorting orders based on the columns, then I would do this from the Dropdown instead.

Set the Items property of your SorterOptions to:

 

Table({Value:"Title", order:SortOrder.Ascending},
      {Value:"Status", order:SortOrder.Ascending},
      {Value:"Created", order:SortOrder.Descending}
)

 

 

Now for your formula with the sorting:

 

SortByColumns(
   yourItems,
   SorterOptions.Selected.Value,
   SorterOptions.Selected.order
)

 

 

I hope this is helpful for you.

 

EDIT: Also, you should avoid using the SelectedText property of the dropdowns - it is a deprecated property and should not be used.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

@RandyHayes Thank you!

1) I can't get "Status" to work, it just returns no results. The other 2 options are working fine

2) Is there a way I can have different values, in my dropdown I want an option "Oprettet" (Danish word) which should be = "Created" in my sharepoint list

@GarethPrisk Thank you!

I can get your solution to work, but can't figure out how to combine it with my Search and Filter.

 

My original code:

 

Sort(
Search(
If(
//all and all
FilterAnsvarlig.Selected.Result = "Alle"; Filter('database'; Status.Value in StatusValg.Result);
//all status and person
FilterAnsvarlig.Selected.Result <> "Alle"; Filter('database';Ansvarlig.Value = FilterAnsvarlig.Selected.Result And Status.Value in StatusValg.Result)
);
SearchField.Text; "Title"; "Description");
If(
SorterOptions.SelectedText.Value = "Title", Title,
SorterOptions.SelectedText.Value = "Status", Status.Value,
SorterOptions.SelectedText.Value = "Created", Value(Created)
)
)

No problem with the delegation warning, my list will never contain more then 100 items.

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

secondImage

Demo Extravaganza is Back!

We are excited to announce that Demo Extravaganza for 2021 has started!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Power Apps June 2021

June Power Apps Community Call

Don't miss the call this month on June 16th - 8a PDT

Top Solution Authors
Top Kudoed Authors
Users online (29,119)