0

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?

star
  • 1,493
  • 1
  • 28
  • 61

1 Answers1

5

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)
Harshank Bansal
  • 2,798
  • 2
  • 7
  • 22
  • On the other scenario instead of DateTime, I'm having only Date (To orderBy). If I change the above to script to orderBy -($$ as String as Date). It is not working as it is expecting a Number because of `-`. Thoughts? – star Nov 05 '22 at 11:47
  • You need to give the format of a Date or DateTime if is it not in ISO format. Refer this answer https://stackoverflow.com/a/74268170/10946202 – Harshank Bansal Nov 05 '22 at 12:04
  • Thanks. I tried this script orderBy -($$ as String as Date {format: "yyyy-MM-dd"}), it is not like the format though as `-` expects a number. – star Nov 05 '22 at 22:40
  • Also tried other ways. Either it complains - or date format. Thoughts? – star Nov 05 '22 at 22:47
  • 1
    @star, in the case of Date, you can just remove the dashes and take that as the number to then add the minus. – Jorge Garcia Nov 07 '22 at 14:04