Devendra
Devendra

Reputation: 221

transformation with order by in mule 4 with dataweave 2.0

I've to make my payload array in ascending order by distanceInKm. This is payload:

 [{
   "id": 1111,
   "accountNumber": 17694838,   
   "pickup": {
    "time": 1580637133000,
    "distanceInKm": 4.3
   }
 },{
  "id": 2222,
  "accountNumber": 17694838,
  "pickup": {
    "time": 1580637133000,
    "distanceInKm": 2.0
  }
},{
  "id": 3333,
  "accountNumber": 17694838,    
  "pickup": {
    "time": 1580637133000,
    "distanceInKm": 4.3
  }
  },{
  "id": 4444,
  "accountNumber": 17694838,
  "pickup": {
    "time": 1580637133000,
    "distanceInKm": 2.0
   }}]

Transformation:

  %dw 2.0
  output application/json 
   ---
 payload map (payload01, index) -> {
id: payload01.id,
Account: payload01.accountNumber,
pickup: {
    time: payload01.pickup.time,
    distance: payload01.pickup.distanceInKm
  }
} orderBy (payload01.pickup.distanceInKm)

I'm doing transformation that is required and after that i'm applying orderBy but that is not working. what changes i can make to make it in ascending order distanceInKm?

Upvotes: 1

Views: 2043

Answers (1)

oim
oim

Reputation: 1151

You can just enclose your "map" inside a group to make sure that what you are ordering by is already the resulting array. See below.

(payload map (payload01, index) -> {
    id: payload01.id,
    Account: payload01.accountNumber,
    pickup: {
        time: payload01.pickup.time,
        distance: payload01.pickup.distanceInKm
    }
}) orderBy $.pickup.distance

This will result to an array ordered by distanceInKm in ascending order. If you want descending order then you can just use -$.pickup.distance as your criteria.

Upvotes: 1

Related Questions