Nav

orderBy

orderBy(O, (V, K) → R): O

Reorders the content of an array or object using a value returned by a function. The function (a lambda) can be invoked with these parameters: value and index.

If the parameters are not named, the index is defined by default as $$ and the value as $.

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
orderByLetter: [{ letter: "d" }, { letter: "e" }, { letter: "c" }, { letter: "a" }, { letter: "b" }] orderBy($.letter)
Output

        
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
  "orderByLetter": [
    {
      "letter": "a"
    },
    {
      "letter": "b"
    },
    {
      "letter": "c"
    },
    {
      "letter": "d"
    },
    {
      "letter": "e"
    }
  ]
}

Note that orderBy($.letter) above produces the same result as orderBy($[0]).

The orderBy function does not have an option to order in descending order instead of ascending. In these cases, you can simply invert the order of the resulting array using -, for example:

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
orderDescending: ([3,8,1] orderBy -$)
Output

        
     
1
{ "orderDescending": [8,3,1] }
orderBy(Array<T>, (T, Number) → R): Array<T>

Sorts an array using the specified criteria.

Transform

        
     
1
2
3
4
%dw 2.0
output application/json
---
[3,2,3] orderBy $
Output

        
     
1
2
3
4
5
[
  2,
  3,
  3
]