---
schema_version: "1"
id: 8b1f67244213d50d0012f1895ac001d398126fced4dd0342641405864ccd9fc2
title: Precedence in DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dataweave-flow-control-precedence.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dataweave-flow-control-precedence
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 11dfdd4a5a0283d87b4c477d2d6093cbc5aa0cfc01bd41cea0de6478df38fa0c
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
---

# Precedence in DataWeave

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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):

<table><colgroup><col> <col></colgroup><thead><tr><th>Level</th><th>Operator or Function</th></tr></thead><tbody><tr><td><p>1</p></td><td><div><div><ul><li><p><code>using</code>: For initializing local variables in a DataWeave script.</p></li><li><p>Unary DataWeave operators at this level:</p><div><ul><li><p><code>.^</code>: Schema selector.</p></li><li><p><code>.#</code>: Namespace selector.</p></li><li><p><code>..</code>: Descendants selector.</p></li><li><p><code>not</code>: Logical operator for negation.</p></li><li><p><code>.@</code>: All attribute selector, for example, in a case that uses the expression <code>payload.root.a.@</code> to return the attributes and values of the input payload <code>&lt;root&gt; &lt;a attr1="foo" attr2="bar" /&gt;&lt;/root&gt;</code> within the array of objects <code>{ "attr1": "foo", "attr2": "bar" }</code>.</p></li></ul></div></li></ul></div></div></td></tr><tr><td><p>2</p></td><td><div><div><ul><li><p><code>as</code>: Used for type coercion.</p></li></ul></div></div></td></tr><tr><td><p>3</p></td><td><div><div><ul><li><p><code>*</code>: Multiply.</p></li><li><p><code>/</code>: Divide.</p></li></ul></div></div></td></tr><tr><td><p>4</p></td><td><div><div><ul><li><p><code>+</code>: Add.</p></li><li><p><code>-</code>: Subtract.</p></li><li><p><code>&gt;&gt;</code>: Prepend data.</p></li><li><p><code>&lt;&lt;</code>: Append data.</p></li></ul></div></div></td></tr><tr><td><p>5</p></td><td><div><div><ul><li><p><code>&gt;</code>: Greater than or equal to.</p></li><li><p><code>=</code>: Equal to.</p></li><li><p><code>&lt;</code>: Less than or equal to.</p></li><li><p><code>&lt;</code>: Less than.</p></li><li><p><code>&gt;</code>: Greater than.</p></li><li><p><code>is</code>: Compare type, for example, used in <a href="https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-remove-certain-xml-attributes">Remove Specified XML Attributes</a>.</p></li></ul></div></div></td></tr><tr><td><p>6</p></td><td><div><div><ul><li><p><code>!=</code>: Not equal to.</p></li><li><p><code>~=</code>: Contains.</p></li><li><p><code>==</code>: Equality operator that tries to coerce one value to the type of the other when the types are different.</p></li></ul></div></div></td></tr><tr><td><p>7</p></td><td><div><div><ul><li><p><code>and</code>: Logical <code>and</code> operator.</p></li></ul></div></div></td></tr><tr><td><p>8</p></td><td><div><div><ul><li><p><code>or</code>: Logical <code>or</code> operator.</p></li></ul></div></div></td></tr><tr><td><p>9</p></td><td><div><div><ul><li><p><code>default</code>: For setting a default value.</p></li><li><p><code>case</code> and <code>else</code>: For pattern matching.</p></li><li><p>DataWeave functions at this level:</p><div><ul><li><p><code>matches</code></p></li><li><p><code>match</code></p></li><li><p><code>map</code></p></li><li><p><code>mapObject</code></p></li><li><p><code>groupBy</code></p></li><li><p><code>filter</code></p></li></ul></div></li><li><p>Binary DataWeave selectors at this level:</p><div><ul><li><p><code>.@keyName</code> and <code>.*@</code>: Binary DataWeave attribute selectors.</p></li><li><p><code>.*</code>: Multi-value selector.</p></li><li><p><code>.^</code>: Metadata selector.</p></li><li><p><code>.&amp;</code>: Key-value pair selector.</p></li><li><p><code>[?()]</code>: Filter selector.</p></li></ul></div></li><li><p><code>.keyName</code>: Value selector.</p></li><li><p><code>[&lt;index&gt; to &lt;index&gt;]</code>: Range selector</p></li></ul></div></div></td></tr><tr><td><p>10</p></td><td><div><div><ul><li><p><code>if else</code>: Conditional expression used for flow control in DataWeave.</p></li></ul></div></div></td></tr></tbody></table>

## 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:

```DataWeave
%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:

```DataWeave
%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](https://docs.mulesoft.com/dataweave/2.6/dw-core)

- [DataWeave Operators](https://docs.mulesoft.com/dataweave/2.6/dw-operators)

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.6/dataweave-selectors)

- [DataWeave Variables](https://docs.mulesoft.com/dataweave/2.6/dataweave-variables)

- [Flow Control in DataWeave](https://docs.mulesoft.com/dataweave/2.6/dataweave-flow-control)

- [Pattern Matching in DataWeave](https://docs.mulesoft.com/dataweave/2.6/dataweave-pattern-matching)

- [Type Coercion with DataWeave](https://docs.mulesoft.com/dataweave/2.6/dataweave-types-coercion)
