Sortノードは、配列やメッセージシーケンスを並べ替えるノードです。
身近な例でいうと、図書館の司書が本を著者名順やタイトル順に棚へ並べ替えるような作業にあたります。
Sortノードは、配列またはメッセージシーケンスを受け取り、指定した条件で並べ替えて出力します。
文字列順、数値順、オブジェクトのプロパティ順など、さまざまな方法でソートできます。
📋 サンプルフロー(クリックで展開)
参照元:NodeREDエディター内サンプルフロー
[
{
"id": "9b4586fe59798b63",
"type": "tab",
"label": "sort",
"disabled": false,
"info": "",
"env": []
},
{
"id": "6451c8bb.b52278",
"type": "sort",
"z": "9b4586fe59798b63",
"name": "",
"order": "ascending",
"as_num": false,
"target": "payload",
"targetType": "msg",
"msgKey": "",
"msgKeyType": "elem",
"seqKey": "payload",
"seqKeyType": "msg",
"x": 570,
"y": 200,
"wires": [["cb34307c.ac1dd"]]
},
{
"id": "638546c.38f1fb8",
"type": "inject",
"z": "9b4586fe59798b63",
"name": "",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 280,
"y": 200,
"wires": [["db5d90ac.bbb3f"]]
},
{
"id": "3ec02cae.012ce4",
"type": "comment",
"z": "9b4586fe59798b63",
"name": "Sort array payload as string in ascending order",
"info": "",
"x": 340,
"y": 140,
"wires": []
},
{
"id": "db5d90ac.bbb3f",
"type": "template",
"z": "9b4586fe59798b63",
"name": "data",
"field": "payload",
"fieldType": "msg",
"format": "json",
"syntax": "mustache",
"template": "[\"orange\", \"banana\", \"apple\", \"pear\", \"kiwi\"]",
"output": "json",
"x": 430,
"y": 200,
"wires": [["6451c8bb.b52278"]]
},
{
"id": "cb34307c.ac1dd",
"type": "debug",
"z": "9b4586fe59798b63",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 730,
"y": 200,
"wires": []
},
{
"id": "735ec14e.4ed55",
"type": "sort",
"z": "9b4586fe59798b63",
"name": "",
"order": "descending",
"as_num": false,
"target": "payload",
"targetType": "msg",
"msgKey": "",
"msgKeyType": "elem",
"seqKey": "payload",
"seqKeyType": "msg",
"x": 570,
"y": 360,
"wires": [["e8dc4ae5.f08598"]]
},
{
"id": "c8ce4e74.9db68",
"type": "inject",
"z": "9b4586fe59798b63",
"name": "",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 280,
"y": 360,
"wires": [["ea0384a6.1346b8"]]
},
{
"id": "70333397.d78f6c",
"type": "comment",
"z": "9b4586fe59798b63",
"name": "Sort array payload as string in descending order",
"info": "",
"x": 340,
"y": 300,
"wires": []
},
{
"id": "ea0384a6.1346b8",
"type": "template",
"z": "9b4586fe59798b63",
"name": "data",
"field": "payload",
"fieldType": "msg",
"format": "json",
"syntax": "mustache",
"template": "[\"orange\", \"banana\", \"apple\", \"pear\", \"kiwi\"]",
"output": "json",
"x": 430,
"y": 360,
"wires": [["735ec14e.4ed55"]]
},
{
"id": "e8dc4ae5.f08598",
"type": "debug",
"z": "9b4586fe59798b63",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 730,
"y": 360,
"wires": []
},
{
"id": "d4b49c22.32685",
"type": "sort",
"z": "9b4586fe59798b63",
"name": "",
"order": "ascending",
"as_num": true,
"target": "payload",
"targetType": "msg",
"msgKey": "",
"msgKeyType": "elem",
"seqKey": "payload",
"seqKeyType": "msg",
"x": 570,
"y": 520,
"wires": [["45738f07.16416"]]
},
{
"id": "87ce9955.924868",
"type": "inject",
"z": "9b4586fe59798b63",
"name": "",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 280,
"y": 520,
"wires": [["30b81283.f0772e"]]
},
{
"id": "f6e624df.703a88",
"type": "comment",
"z": "9b4586fe59798b63",
"name": "Sort array payload as number",
"info": "",
"x": 280,
"y": 460,
"wires": []
},
{
"id": "30b81283.f0772e",
"type": "template",
"z": "9b4586fe59798b63",
"name": "data",
"field": "payload",
"fieldType": "msg",
"format": "json",
"syntax": "mustache",
"template": "[\"1024\", \"86\", \"256\", \"100\", \"9\"]",
"output": "json",
"x": 430,
"y": 520,
"wires": [["d4b49c22.32685"]]
},
{
"id": "45738f07.16416",
"type": "debug",
"z": "9b4586fe59798b63",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 730,
"y": 520,
"wires": []
},
{
"id": "b91c19b1.b66b18",
"type": "sort",
"z": "9b4586fe59798b63",
"name": "",
"order": "ascending",
"as_num": false,
"target": "payload",
"targetType": "msg",
"msgKey": "price",
"msgKeyType": "jsonata",
"seqKey": "payload",
"seqKeyType": "msg",
"x": 570,
"y": 680,
"wires": [["32dd80a1.226e4"]]
},
{
"id": "adb0daa2.d85a48",
"type": "inject",
"z": "9b4586fe59798b63",
"name": "",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 280,
"y": 680,
"wires": [["b3ee1b9e.dbf388"]]
},
{
"id": "d1190f8b.9a74b",
"type": "comment",
"z": "9b4586fe59798b63",
"name": "Sort array of objects using JSONata (price)",
"info": "",
"x": 340,
"y": 620,
"wires": []
},
{
"id": "b3ee1b9e.dbf388",
"type": "template",
"z": "9b4586fe59798b63",
"name": "data",
"field": "payload",
"fieldType": "msg",
"format": "json",
"syntax": "mustache",
"template": "[{\"name\": \"orange\", \"price\": 80}, {\"name\": \"banana\", \"price\": 250}, {\"name\": \"apple\", \"price\": 100}]",
"output": "json",
"x": 430,
"y": 680,
"wires": [["b91c19b1.b66b18"]]
},
{
"id": "32dd80a1.226e4",
"type": "debug",
"z": "9b4586fe59798b63",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 730,
"y": 680,
"wires": []
}
]
対象: msg.payload
順序: 昇順
数値として比較: OFF
入力: ["orange", "banana", "apple", "kiwi"]
出力: ["apple", "banana", "kiwi", "orange"]
対象: msg.payload
順序: 降順
数値として比較: OFF
入力: ["orange", "banana", "apple", "kiwi"]
出力: ["pear", "orange", "kiwi", "banana", "apple"]
対象: msg.payload
順序: 昇順
数値として比較: ON
入力: ["1024", "86", "256", "100", "9"]
出力: ["9", "86", "100", "256", "1024"]
対象: msg.payload
キー: price (JSONata式)
順序: 昇順
入力: [
{"name": "orange", "price": 80},
{"name": "banana", "price": 250},
{"name": "apple", "price": 100}
]
出力: [
{"name": "orange", "price": 80},
{"name": "apple", "price": 100},
{"name": "banana", "price": 250}
]
対象: msg.payload
キー: $substring("0000" & $string(price), -4) & name
順序: 昇順
この式の意味:
1. priceを4桁のゼロ埋め文字列に変換(100 → "0100")
2. nameを連結("0100apple")
3. この連結文字列でソート
結果: 価格順、同じ価格なら名前順
対象: メッセージシーケンス
キー: msg.payload
順序: 昇順
入力シーケンス: "orange" → "banana" → "apple" → "kiwi"
出力シーケンス: "apple" → "banana" → "kiwi" → "orange"
演習4: メッセージシーケンスのソート上級
課題
Splitで分割したメッセージシーケンスをソートし、Joinで再結合してください。
💼 使いどころ: 改行区切りのリストを分割して並べ替え、一つの配列にまとめる場面で使います。
要求仕様
- Inject、Template、Split、Sort、Join、Debugノードを使用
- 入力: 改行区切りの都市名リスト
- 出力: アルファベット順に並べ替えられた配列
期待される動作
- Splitで分割 → Sortで並べ替え → Joinで配列に再結合
成功の条件
- デバッグパネルに1つのメッセージとして配列が表示される
- 出力配列が ["Kyoto", "Nagoya", "Osaka", "Tokyo"] のアルファベット昇順になっている
- 配列の先頭が "Kyoto"、末尾が "Tokyo" になっている
- Joinによってメッセージシーケンスが1つの配列に再結合されている
💡 ヒント
Templateノードの設定:
- 形式: テキスト
- テンプレート: Tokyo, Osaka, Kyoto, Nagoyaを改行で区切って入力
- 出力: 文字列
Splitノードの設定:
Sortノードの設定:
- 対象: メッセージシーケンス
- キー: msg.payload
- 順序: 昇順
Joinノードの設定:
✅ 解答例フロー
[
{
"id": "sort_node_guide_tab_4",
"type": "tab",
"label": "sort_node_guide 例5",
"disabled": false,
"info": "",
"env": []
},
{
"id": "ex4_inject",
"type": "inject",
"z": "sort_node_guide_tab_4",
"name": "実行",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 90,
"y": 100,
"wires": [
[
"ex4_template"
]
]
},
{
"id": "ex4_template",
"type": "template",
"z": "sort_node_guide_tab_4",
"name": "都市リスト",
"field": "payload",
"fieldType": "msg",
"format": "text",
"syntax": "plain",
"template": "Tokyo\nOsaka\nKyoto\nNagoya",
"output": "str",
"x": 220,
"y": 100,
"wires": [
[
"ex4_split"
]
]
},
{
"id": "ex4_split",
"type": "split",
"z": "sort_node_guide_tab_4",
"name": "分割",
"splt": "\\n",
"spltType": "str",
"arraySplt": 1,
"arraySpltType": "len",
"stream": false,
"addname": "",
"x": 350,
"y": 100,
"wires": [
[
"ex4_sort"
]
]
},
{
"id": "ex4_sort",
"type": "sort",
"z": "sort_node_guide_tab_4",
"name": "ソート",
"order": "ascending",
"as_num": false,
"target": "payload",
"targetType": "seq",
"msgKey": "",
"msgKeyType": "elem",
"seqKey": "payload",
"seqKeyType": "msg",
"x": 470,
"y": 100,
"wires": [
[
"ex4_join"
]
]
},
{
"id": "ex4_join",
"type": "join",
"z": "sort_node_guide_tab_4",
"name": "結合",
"mode": "auto",
"build": "array",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "",
"count": "",
"reduceRight": false,
"x": 590,
"y": 100,
"wires": [
[
"ex4_debug"
]
]
},
{
"id": "ex4_debug",
"type": "debug",
"z": "sort_node_guide_tab_4",
"name": "結果",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"x": 710,
"y": 100,
"wires": []
}
]