web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id : NyqQKMoL6Y8jadkzNCtEwt
Power Apps - Building Power Apps
Unanswered

Sort() not sorting Numbers correctly with a Switch() Statement

Like (1) ShareShare
ReportReport
Posted on 18 Feb 2022 14:37:53 by 2

Hi,

 

I am trying to have dynamic sorting, with complex types which is why SortByColumn() is not working.

 

My code basically is this:

 

 

Sort('Data';
Switch(lastSortFilter;
 "xy";'xy.DisplayName;
 "ID"; ID;
 "Title";Title;
 "Status";Status.Value;
 "abc";'abc'.Value;
)
 ; If(SortVar; Descending; Ascending))


	

 

 

The problem is, it is only working as text sort an breaks the sorting of the number columns. For Example ID gets sortet like this:

1
10
11

2

 

But ID is not a text column. If I change my Code to this:

 

Sort('Data';
ID
 ; If(SortVar; Descending; Ascending))

 

It sorts the way it should be. So 1, 2, [...], 10, 11.

 

So why is the Switch() function breaking the sort function and is there any workaround?

 

Thanks for any help

  • timl Profile Picture
    35,437 Super User 2025 Season 2 on 25 Feb 2022 at 11:21:36
    Re: Sort() not sorting Numbers correctly with a Switch() Statement

    Hi @tryingPwer 

    The return value from Switch must always return a value with a consistant data type - in this case, a string.

    A workaround would be to format the id values with preceeding spaces using the Text function (eg, 00001, 00002, 00003) etc, and the sort should then work as expected.

    Sort('Data';
     Switch(lastSortFilter;
     "xy";'xy.DisplayName;
     "ID"; Text(ID;"00000#");
     "Title";Title;
     "Status";Status.Value;
     "abc";'abc'.Value;
     );
     If(SortVar; Descending; Ascending)
    )
    

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Telen Wang – Community Spotlight

We are honored to recognize Telen Wang as our August 2025 Community…

Announcing our 2025 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 637 Most Valuable Professional

#2
stampcoin Profile Picture

stampcoin 570 Super User 2025 Season 2

#3
Power Apps 1919 Profile Picture

Power Apps 1919 473

Loading complete