---
schema_version: "1"
id: 9d0cd2addf06c394f391771e08845f4cd13112395157af89beb69bcbefcaee51
title: orderBy
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.5/dw-core-functions-orderby.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.5/dw-core-functions-orderby
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2794d40a5417a94506f3d08ee2c93d5f3295109b24ea992887051e33248073dc
description: MuleSoft Documentation Site
release: "2.5"
latest: false
---
# orderBy

## [](#orderby1)orderBy<K, V, R, O <: { (K)?: V }>(object: O, criteria: (value: V, key: K) -> R): O

入力の要素を、その入力の選択された要素で動作する条件を使用して並び替えます。

このバージョンの ​`orderBy`​ は、入力としてオブジェクトを使用します。その他のバージョンは、入力配列で動作するか、​`null`​ 値を処理します。

インデックスは匿名パラメーター ​`$$`​、値は ​`$`​ で参照できます。

### [](#パラメーター)パラメーター

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>object</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">並び替えるオブジェクト。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>criteria</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">関数の結果は、オブジェクトを並び替える条件として使用されます。</p></td>
</tr>
</tbody>
</table>

### [](#例)例

次の例では、入力配列内の各オブジェクトの値をアルファベット順に並べます。​`orderBy($.letter)`​ は ​`orderBy($[0])`​ と同じ結果になります。

#### [](#ソース)ソース

```DataWeave
%dw 2.0
output application/json
---
{ letter: "b", letter: "c", letter: "a" } orderBy ((value, key) -> value)
```

#### [](#出力)出力

```JSON
{
  "letter": "a",
  "letter": "b",
  "letter": "c"
}
```

## [](#orderby2)orderBy<T, R>(array: Array<T>, criteria: (item: T, index: Number) -> R): Array<T>

指定された条件を使用して配列を並び替えます。

### [](#パラメーター-2)パラメーター

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">名前</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>array</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">並び替える配列。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>criteria</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">関数の結果は、配列を並び替える条件として機能します。単純な値 (​<code>String</code>​、​<code>Number</code>​、など) を返す必要があります。</p></td>
</tr>
</tbody>
</table>

### [](#例-2)例

次の例では、数値に基づいて数値の配列を並び替えます。

#### [](#ソース-2)ソース

```DataWeave
%dw 2.0
output application/json
---
[3,2,3] orderBy $
```

#### [](#出力-2)出力

```JSON
[ 2, 3, 3 ]
```

### [](#例-3)例

`orderBy`​ 関数には、昇順ではなく降順で並び替えるオプションはありません。この場合は、たとえば ​`-`​ を使用して、結果の配列の順序を単に反転することができます。

#### [](#ソース-3)ソース

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

#### [](#出力-3)出力

```JSON
{ "orderDescending": [8,3,1] }
```

### [](#例-4)例

次の例では、age (年齢) に基づいて人の配列を並び替えます。

#### [](#ソース-4)ソース

```DataWeave
%dw 2.0
output application/json
---
[{name: "Santiago", age: 42},{name: "Leandro", age: 29}, {name: "Mariano", age: 35}] orderBy (person) -> person.age
```

#### [](#出力-4)出力

```JSON
[
  {
    name: "Leandro",
    age: 29
  },
  {
    name: "Mariano",
    age: 35
  },
  {
    name: "Santiago",
    age: 42
  }
]
```

### [](#例-5)例

次の例では、DateTime の配列を降順に並び替えます。

#### [](#ソース-5)ソース

```DataWeave
%dw 2.0
output application/json
---
[|2020-10-01T23:57:59.017Z|, |2022-12-22T12:12:12.011Z|, |2020-10-01T12:40:10.012Z|, |2020-10-01T23:57:59.021Z|]
  orderBy -($ as Number {unit: "milliseconds"})
```

#### [](#出力-5)出力

```JSON
[
  "2022-12-22T12:12:12.011Z",
  "2020-10-01T23:57:59.021Z",
  "2020-10-01T23:57:59.017Z",
  "2020-10-01T12:40:10.012Z"
]
```

### [](#例-6)例

この例は、JSON 配列内のオブジェクトの順序を変更します。この式は、最初に ​`Type`​ キーの値をアルファベット順に並び替えてから、​`[-1 to 0]`​ に基づいて順序を逆にします。

#### [](#ソース-6)ソース

```DataWeave
%dw 2.0
var myInput = [
    {
        "AccountNumber": "987999321",
        "NameOnAccount": "QA",
        "Type": "AAAA",
        "CDetail": {
            "Status": "Open"
        }
    },
    {
        "AccountNumber": "12399978",
        "NameOnAccount": "QA",
        "Type": "BBBB",
        "CDetail": {}
    },
    {
        "AccountNumber": "32199974",
        "NameOnAccount": "QA",
        "Type": "CCCC",
        "CDetail": {}
    }
]
output application/json
---
(myInput orderBy $.Type)[-1 to 0]
```

#### [](#出力-6)出力

```JSON
[
  {
    "AccountNumber": "32199974",
    "NameOnAccount": "QA",
    "Type": "CCCC",
    "CDetail": {

    }
  },
  {
    "AccountNumber": "12399978",
    "NameOnAccount": "QA",
    "Type": "BBBB",
    "CDetail": {

    }
  },
  {
    "AccountNumber": "987999321",
    "NameOnAccount": "QA",
    "Type": "AAAA",
    "CDetail": {
      "Status": "Open"
    }
  }
]
```

## [](#orderby3)orderBy(value: Null, criteria: (item: Nothing, index: Nothing) -> Null): Null

`orderBy`​ を ​`null`​ 値に使用できるようにするヘルパー関数。