Precedence in DataWeave | MuleSoft Documentation
# Precedence in DataWeave
DataWeave expressions are compiled in a specific order. The result of a compilation of something at one level can serve as input for expressions in higher levels, but not at lower levels.
## Order of Compilation
The following table orders operators and functions from first compiled (1) to last compiled (10):
Level | Operator or Function  
---|---  
  * `using`: For initializing local variables in a DataWeave script.
  * Unary DataWeave operators at this level:
    * `.^`: Schema selector.
    * `.#`: Namespace selector.
    * `..`: Descendants selector.
    * `not`: Logical operator for negation.
    * `.@`: All attribute selector, for example, in a case that uses the expression `payload.root.a.@` to return the attributes and values of the input payload `<root> <a attr1="foo" attr2="bar" /></root>` within the array of objects `{ "attr1": "foo", "attr2": "bar" }`.

  
  * `as`: Used for type coercion.

  
  * `*`: Multiply.
  * `/`: Divide.

  
  * `+`: Add.
  * `-`: Subtract.
  * : Prepend data.
  * : Append data.

  
  * : Greater than or equal to.
  * `=`: Equal to.
  * : Less than or equal to.
  * : Less than.
  * : Greater than.
  * `is`: Compare type, for example, used in Remove Specified XML Attributes.

  
  * `!=`: Not equal to.
  * `~=`: Contains.
  * `==`: Equality operator that tries to coerce one value to the type of the other when the types are different.

  
  * `and`: Logical `and` operator.

  
  * `or`: Logical `or` operator.

  
  * `default`: For setting a default value.
  * `case` and `else`: For pattern matching.
  * DataWeave functions at this level:
    * `matches`
    * `mapObject`
    * `groupBy`
  * Binary DataWeave selectors at this level:
    * `.@keyName` and `.*@`: Binary DataWeave attribute selectors.
    * `.*`: Multi-value selector.
    * `.^`: Metadata selector.
    * : Key-value pair selector.
    * `[?()]`: Filter selector.
  * `.keyName`: Value selector.
  * `[<index> to <index>]`: Range selector

  
  * `if else`: Conditional expression used for flow control in DataWeave.

  
## Order of Chained Function Calls
When functions are chained together in a series, the functions are processed in order from the first to the last function specified in the chain.
For example, the following script acts on the input array of objects defined by the variable `flights`. The script first calls `filter` to return an array that accepts all `price` values that are less than 500. Then it calls `orderBy` to return an array that reorders the objects found in the input from the lowest to the highest `price` value. Finally, it calls `groupBy` to group the input array of objects alphabetically by `toAirport` value.
DataWeave Script:
```
%dw 2.0
output application/json
var flights = [
 { "toAirport": "SFO", "price": 550, "airline": "American" },
 { "toAirport": "MUA", "price": 200, "airline": "American" },
 { "toAirport": "SFO", "price": 300, "airline": "American" },
 { "toAirport": "CLE", "price": 600, "airline": "American" },
 { "toAirport": "CLE", "price": 190, "airline": "American" },
 { "toAirport": "SFO", "price": 400, "airline": "American" }
]
---
flights filter $.price < 500 orderBy $.price groupBy $.toAirport
```

The result is an object that contains a collection of key-value pairs:
Output:
```
{
 "CLE": [
  {
   "toAirport": "CLE",
   "price": 190,
   "airline": "American"
  }
 ],
 "SFO": [
  {
   "toAirport": "SFO",
   "price": 300,
   "airline": "United"
  },
  {
   "toAirport": "SFO",
   "price": 400,
   "airline": "American"
  }
 ],
 "MUA": [
  {
   "toAirport": "MUA",
   "price": 200,
   "airline": "American"
  }
 ]
}
```

You can force the evaluation order by inserting evaluation parentheses `(` and `)` into the DataWeave expression. This example returns the same output as the previous example:
DataWeave Script:
```
%dw 2.0
output application/json
var flights = [
 { "toAirport": "SFO", "price": 550, "airline": "American" },
 { "toAirport": "MUA", "price": 200, "airline": "American" },
 { "toAirport": "SFO", "price": 300, "airline": "American" },
 { "toAirport": "CLE", "price": 600, "airline": "American" },
 { "toAirport": "CLE", "price": 190, "airline": "American" },
 { "toAirport": "SFO", "price": 400, "airline": "American" }
]
---
( ( ( flights filter $.price < 500 ) orderBy $.price ) groupBy $.toAirport )
```

The result is an object that contains a collection of key-value pairs:
Output:
```
{
 "CLE": [
  {
   "toAirport": "CLE",
   "price": 190,
   "airline": "American"
  }
 ],
 "SFO": [
  {
   "toAirport": "SFO",
   "price": 300,
   "airline": "United"
  },
  {
   "toAirport": "SFO",
   "price": 400,
   "airline": "American"
  }
 ],
 "MUA": [
  {
   "toAirport": "MUA",
   "price": 200,
   "airline": "American"
  }
 ]
}
```

The order of the function calls is important. For example, the rearranged expression `flights groupBy $.toAirport filter $.price < 500 orderBy $.price` returns an error because `groupBy` returns an object, and `filter` expects an array. The resulting error is `Expecting Type: Array>T<, but got: {String: Array<{|toA…​ airline: String|}>}`.
## See Also
  * Core (dw::Core) functions
  * DataWeave Operators
  * DataWeave Selectors
  * DataWeave Variables
  * Flow Control in DataWeave
  * Pattern Matching in DataWeave
  * Type Coercion with DataWeave