📋 サンプルフロー(クリックで展開)
参照元: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"