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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

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:
  • 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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 289

#2
11manish Profile Picture

11manish 288 Super User 2026 Season 2

#3
Henil Profile Picture

Henil 125

Last 30 days Overall leaderboard