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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Sort JSON array by two...
Power Automate
Unanswered

Sort JSON array by two columns (with DESC)

(0) ShareShare
ReportReport
Posted on by 31

Hi,

I have simple task yet hard to implement, I need to sort an array by multiple columns but one of them needs to be DESC which always mess up everything using `reverse` expression (including this solution with concat and select action, which is actually pretty clever) 

 

Desired sorting: Incidents DESC, Department ASC

 

Example input:

 

[
 { "Department": "Department A", "Incidents": 10},
 { "Department": "Department D", "Incidents": 0},
 { "Department": "Department B", "Incidents": 0},
 { "Department": "Department C", "Incidents": 11},
 { "Department": "Department E", "Incidents": 258}
]

 

Desired output:

 

[
 { "Department": "Department E", "Incidents": 258},
 { "Department": "Department C", "Incidents": 11},
 { "Department": "Department A", "Incidents": 10},
 { "Department": "Department B", "Incidents": 0},
 { "Department": "Department D", "Incidents": 0}
]

 

 

 

Categories:
I have the same question (0)
  • Verified answer
    HamidBee Profile Picture
    932 Super User 2024 Season 1 on at

    You can try this:

    1. Use the sort() method to sort the array by the first column (Department) in ascending order.
    2. Use the reverse() method to reverse the sorted array so that it is in descending order.
    3. Use the sort() method again to sort the array by the second column (Incidents) in descending order.


    Here is an example code snippet that sorts the input array as per your desired sorting:

     

     

    const input = [
    { "Department": "Department A", "Incidents": 10},
    { "Department": "Department D", "Incidents": 0},
    { "Department": "Department B", "Incidents": 0},
    { "Department": "Department C", "Incidents": 11},
    { "Department": "Department E", "Incidents": 258}
    ];
    
    input.sort((a, b) => {
    if (a.Department < b.Department) {
    return -1;
    } else if (a.Department > b.Department) {
    return 1;
    } else {
    return b.Incidents - a.Incidents;
    }
    });
    
    console.log(input);

     


    The output of this code snippet will be:

     

     

    [
    { Department: 'Department E', Incidents: 258 },
    { Department: 'Department C', Incidents: 11 },
    { Department: 'Department A', Incidents: 10 },
    { Department: 'Department B', Incidents: 0 },
    { Department: 'Department D', Incidents: 0 }
    ]
    

     

  • Popky Profile Picture
    31 on at

    Thank you, 1, 2, 3, (4) steps helped. I would not think of using double 'reverse' expression. 

     

    Solution:

    reverse(sort(reverse(sort(outputs('Compose_-_Array'),'Department')),'Incidents'))

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 522 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 364 Moderator

#3
abm abm Profile Picture

abm abm 243 Most Valuable Professional

Last 30 days Overall leaderboard