Hi There,
I've just created a test flow for testing the functionality of the sort node:
[{"id":"68dcdddf9a7ee3cd","type":"inject","z":"5f6929bb3374b782","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[2,3,1,5]","payloadType":"json","x":336.5,"y":925.5,"wires":[["aa81c2f54b39c43a"]]},{"id":"aa81c2f54b39c43a","type":"split","z":"5f6929bb3374b782","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":462,"y":991,"wires":[["5948a955bd6357a0","959be4a3354c98a1"]]},{"id":"0d9827ef535d0dd3","type":"delay","z":"5f6929bb3374b782","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"100","randomLast":"500","randomUnits":"milliseconds","drop":false,"allowrate":false,"outputs":1,"x":765,"y":1170,"wires":[["3c70b3f97b8d63a0"]]},{"id":"3c70b3f97b8d63a0","type":"sort","z":"5f6929bb3374b782","name":"","order":"ascending","as_num":true,"target":"","targetType":"seq","msgKey":"payload","msgKeyType":"elem","seqKey":"payload","seqKeyType":"msg","x":940,"y":1088,"wires":[["baf71f9b1453486c"]]},{"id":"5948a955bd6357a0","type":"change","z":"5f6929bb3374b782","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"(30 - $$.payload) * 100","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":616,"y":1072,"wires":[["0d9827ef535d0dd3"]]},{"id":"959be4a3354c98a1","type":"debug","z":"5f6929bb3374b782","name":"debug 446","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$$.payload & \" => \" & $$.parts.index","targetType":"jsonata","statusVal":"","statusType":"auto","x":763,"y":948,"wires":[]},{"id":"baf71f9b1453486c","type":"debug","z":"5f6929bb3374b782","name":"debug 447","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"$$.payload & \" => \" & $$.parts.index","targetType":"jsonata","statusVal":"","statusType":"auto","x":1152,"y":1006,"wires":[]}]
What this does is sort the array of values [2,3,1,5]
when this goes through the split node, the 2 gets index 0, the 3 gets index 1 ... as shown in the first four values in the debug log:
What I don't understand are the last four outputs. I configured the sort node to sort by "message sequence" so I would have expected the same order, i.e. [2,3,1,5] but instead the order becomes 1.2.3.5 with the index values modified to match that ordering, i.e. index of 2 becomes 1 (not zero), 3 becomes 2 (not 1).
What I don't understand is why the sort node is doing that?
The configuration of the sort node is:
I would have thought that message sequence is the sorting mechanism and not the payload even though it is the key.
The documentation states:
The only thing between the split and sort nodes is a change & delay nodes to ensure that the messages arrive in reverse order - the change sets the delay according to inverse payload value.
I can fix and get the expected result by setting the key to be a JSONata expression of "$$.parts.index" and then it's sorted correctly but that shouldn't be case - surely.