简体   繁体   English

使用 dataweave 2.0 在 mule 4 中按顺序进行转换

[英]transformation with order by in mule 4 with dataweave 2.0

I've to make my payload array in ascending order by distanceInKm.我必须按 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.我正在做需要的转换,之后我正在应用 orderBy 但这不起作用。 what changes i can make to make it in ascending order distanceInKm?我可以进行哪些更改以使其按 distanceInKm 升序排列?

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.这将导致按 distanceInKm 升序排列的数组。 If you want descending order then you can just use -$.pickup.distance as your criteria.如果你想要降序,那么你可以使用-$.pickup.distance作为你的标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM