# Sort node - I'm stuck!

**URL:** <https://discourse.nodered.org/t/sort-node-im-stuck/37530>\
**Category:** General\
**Created:** [15 December 2020 12:18 UTC](https://discourse.nodered.org/t/sort-node-im-stuck/37530 "2020-12-15T12:18:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FatPilot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatpilot/32/38329_2.png) [@FatPilot](https://discourse.nodered.org/u/FatPilot)\
**Post date:** [15 December 2020 12:18 UTC](https://discourse.nodered.org/t/sort-node-im-stuck/37530/1 "2020-12-15T12:18:35Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6ec6cad78f17dd5229f4aff69d5b036e6a504c51.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 December 2020 14:02 UTC](https://discourse.nodered.org/t/sort-node-im-stuck/37530/2 "2020-12-15T14:02:51Z")

</div>

You can also sort in the change node using JSONata

here is example of both

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 December 2020 14:33 UTC](https://discourse.nodered.org/t/sort-node-im-stuck/37530/3 "2020-12-15T14:33:40Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/5/65521e8e5a93f637b026a1c19fe9585782dbe82c.png)

---

<div class="post-metadata">

**Author:** ![FatPilot](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatpilot/32/38329_2.png) [@FatPilot](https://discourse.nodered.org/u/FatPilot)\
**Post date:** [15 December 2020 15:39 UTC](https://discourse.nodered.org/t/sort-node-im-stuck/37530/4 "2020-12-15T15:39:27Z")

</div>

Yet again - Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 December 2020 15:39 UTC](https://discourse.nodered.org/t/sort-node-im-stuck/37530/5 "2020-12-29T15:39:29Z")

</div>

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