Sort node - I'm stuck!

Hi everyone,
It's me again! Why won't this work?
Using the sort node, I'm trying to sort the array on distance for presentation in a list.

Thanks in advance.
Cheers.
Paul.

You can also sort in the change node using JSONata

here is example of both

[{"id":"47f6c00f.1ece5","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"test\":\"yes\",\"Distance\":240},{\"test\":\"yes\",\"Distance\":220},{\"test\":\"yes\",\"Distance\":200},{\"test\":\"yes\",\"Distance\":240},{\"test\":\"yes\",\"Distance\":260},{\"test\":\"yes\",\"Distance\":215}]","payloadType":"json","x":130,"y":2340,"wires":[["43503e39.ba7c48","e7746005.de6518"]]},{"id":"43503e39.ba7c48","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sort(payload, function($l, $r) {\t  $l.Distance > $r.Distance\t})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":2340,"wires":[["29e561d.0ea579e"]]},{"id":"e7746005.de6518","type":"sort","z":"8d22ae29.7df6d","name":"","order":"ascending","as_num":true,"target":"payload","targetType":"msg","msgKey":"Distance","msgKeyType":"jsonata","seqKey":"payload","seqKeyType":"msg","x":290,"y":2400,"wires":[["29e561d.0ea579e"]]},{"id":"29e561d.0ea579e","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":2340,"wires":[]}]

To expand on the reply from @E1cid, to save others from importing the flow to see what the issue was, the screenshot of the Sort node shows us it has been configured to sort msg.payload.Distance as if it were an array.

However, the array that needs sorting is msg.payload. Setting the "key" field to Distance tells the node what property of the objects in that array to use in order to sort it.

1 Like

Yet again - Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.