Search code examples
muledataweavemule4

Reverse ordering in Mule4


I have a data in similar faashion as given in the dwl script variable.

%dw 2.0
output application/json
var test = { "2022-10-19T10:59:00.000Z":[{"kio":"spotage"}] ,
            "2022-10-17T10:59:00.000Z": [{"kio":"spotage"}] ,
            "2022-10-18T10:59:00.000Z": [{"kio":"spotage"}] 
            }
---
test  orderBy $$

Need to sort by dateformat in ASC order. Getting the below response which is the default ordering DSC. Wanting to reverse it ASC. Tried -$$ and also [-1 to 0] and tried formatting as LocalDateTime check to see if it is working.

    {
  "2022-10-17T10:59:00.000Z": [
    {
      "kio": "spotage"
    }
  ],
  "2022-10-18T10:59:00.000Z": [
    {
      "kio": "spotage"
    }
  ],
  "2022-10-19T10:59:00.000Z": [
    {
      "kio": "spotage"
    }
  ]
}

Expecting response as below

{
    "2022-10-19T10:59:00.000Z": [
        {
            "kio": "spotage"
        }
    ],
    "2022-10-18T10:59:00.000Z": [
        {
            "kio": "spotage"
        }
    ],
    "2022-10-17T10:59:00.000Z": [
        {
            "kio": "spotage"
        }
    ]
}

Please let me know if the question is not clear. Any thoughts?


Solution

  • The type of $$ is Key. Therefore you can not do -$$ directly. You need to convert the keys to String and then to DateTime so that it can properly sort it properly.

    %dw 2.0
    output application/json
    var test = { "2022-10-19T10:59:00.000Z":[{"kio":"spotage"}] ,
                "2022-10-17T10:59:00.000Z": [{"kio":"spotage"}] ,
                "2022-10-18T10:59:00.000Z": [{"kio":"spotage"}] 
                }
    ---
    test orderBy -($$ as String as DateTime)