Sorting the data can be done using dataweave in Mule 4 with orderBy function.
orderBy: Reorders the content of an object using a value returned by a function as the criteria.
Sorting Numbers: In Mule 4 using dataweave.
Ascending order: To sort the numbers in ascending order, use 'orderBy $'.
Example: numberAsc: ([3,8,1,6] orderBy $)
Descending order: To sort the numbers in descending order, use 'orderBy -$'.
Example: numberDesc: ([3,8,1,6] orderBy -$)
Sorting Letters: In Mule 4 using dataweave.
Ascending order: To sort the letters in ascending order, use 'orderBy $' just like in case of sorting the numbers.
Example: letterAsc: (["s","e","p","z","a"] orderBy $)
Descending order: To sort the letters in descending order, there is an extra logic to be used. Arrange the letters in ascending order using 'orderBy $' and on top of that, use '[-1 to 0]', which inverts the ascending order thereby giving the descending order of letters.
Example: letterDesc: (["s","e","p","z","a"] orderBy $)[-1 to 0]
Complete code snippet:
Input Dataweave logic:
Output:
Please feel free to comment your queries/opinions.
orderBy: Reorders the content of an object using a value returned by a function as the criteria.
Sorting Numbers: In Mule 4 using dataweave.
Ascending order: To sort the numbers in ascending order, use 'orderBy $'.
Example: numberAsc: ([3,8,1,6] orderBy $)
Descending order: To sort the numbers in descending order, use 'orderBy -$'.
Example: numberDesc: ([3,8,1,6] orderBy -$)
Sorting Letters: In Mule 4 using dataweave.
Ascending order: To sort the letters in ascending order, use 'orderBy $' just like in case of sorting the numbers.
Example: letterAsc: (["s","e","p","z","a"] orderBy $)
Descending order: To sort the letters in descending order, there is an extra logic to be used. Arrange the letters in ascending order using 'orderBy $' and on top of that, use '[-1 to 0]', which inverts the ascending order thereby giving the descending order of letters.
Example: letterDesc: (["s","e","p","z","a"] orderBy $)[-1 to 0]
Complete code snippet:
Input Dataweave logic:
Output:
Please feel free to comment your queries/opinions.
Nice post useful for everyone.Keep on sharing
ReplyDeleteMulesoft Online Training
Mulesoft Training in Hyderabad