# Payload array - how to sort?

**URL:** <https://discourse.nodered.org/t/payload-array-how-to-sort/29018>\
**Category:** General\
**Created:** [26 June 2020 08:03 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018 "2020-06-26T08:03:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mckenzi](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Mckenzi](https://discourse.nodered.org/u/Mckenzi)\
**Post date:** [26 June 2020 08:03 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/1 "2020-06-26T08:03:45Z")

</div>

I'm looking for a way to play with data from ERP system. Idea is to create a dashboard which shows list of customers and status/qty of orders.

I was able to get the data from Excel file to NR. Structure of excel table and massage payload are show in the picture.

Question is how to sort the data by customers ( column C) and sum q'ty (column N) ?

Thanks for advise.

 ![nrpms](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/331e9900735fce7e4e9bd1a8060125c1e2d68e9d.jpeg)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [26 June 2020 08:19 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/2 "2020-06-26T08:19:28Z")

</div>

search this forum for node-red-node-ui-table and there are lots of examples that come with it.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [26 June 2020 08:20 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/3 "2020-06-26T08:20:09Z")

</div>

Maybe it's easier and nicer to use node-red-node-ui-table.  
With this you can easy sort.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 June 2020 08:52 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/4 "2020-06-26T08:52:36Z")

</div>

If you need to sort and am not using ui-table then the javascript sort() method can be passed a compare function so you can sort on a particular attribute of the structure.  
See [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global\_Objects/Array/sort](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [26 June 2020 09:48 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/5 "2020-06-26T09:48:41Z")

</div>

> [@Mckenzi](#):
>
> Question is how to sort the data by customers ( column C) and sum q'ty (column N) ?

This can also be done in a change node using a 1 line jsonata expression.

See [Path Operators · JSONata](https://docs.jsonata.org/path-operators#---order-by)

Share your payload to be sorted in text format and I (or others) will give you the jsonata expression.

---

<div class="post-metadata">

**Author:** ![Mckenzi](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Mckenzi](https://discourse.nodered.org/u/Mckenzi)\
**Post date:** [26 June 2020 11:36 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/6 "2020-06-26T11:36:40Z")

</div>

Hi janvda ,

Payload in text format attached. My programming skills are not good enough to solve this problem by myself.

[array.txt](https://discourse.nodered.org/uploads/short-url/bgp2k7y7MoDpeqiDae88Zu8zizB.txt) (6.4 KB)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [26 June 2020 12:21 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/7 "2020-06-26T12:21:46Z")

</div>

Hi,

The jsonata expression

```auto
payload^(C,N)

```

See it at work:

- [https://try.jsonata.org/X7B4kFhRj](https://try.jsonata.org/X7B4kFhRj)

Notes:

- I have assumed that the array is stored in the msg.payload.
- I have assumed that you wanted it to be sorted first on column C and then on column N.

The expression can be easily used in a node-red change node to convert the input in sorted output.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/4402b6d3493d58cbe10c21d77029d27c87c17a24.png)

---

<div class="post-metadata">

**Author:** ![Mckenzi](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Mckenzi](https://discourse.nodered.org/u/Mckenzi)\
**Post date:** [26 June 2020 13:40 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/8 "2020-06-26T13:40:05Z")

</div>

Hi,  
Thank janvda, it works.  
I've made a easier table to see better what's really happening.  
[https://try.jsonata.org/X7B4kFhRj](https://try.jsonata.org/X7B4kFhRj)

How to use expresion in switch node ?  
In other words how to swich msg.payload based on customers - to have separate payload of each of them ?

If customer is "a" - one msg , if "b: - second msf. and so on.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42d5c0ba23cc5cb7f138004e72897513f30ff4b4.png)

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [26 June 2020 14:24 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/9 "2020-06-26T14:24:32Z")

</div>

It is not really clear what you want.

Maybe instead of a single switch node you can consider a switch node where each output is followed by a change node.

---

<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:** [25 August 2020 14:24 UTC](https://discourse.nodered.org/t/payload-array-how-to-sort/29018/10 "2020-08-25T14:24:39Z")

</div>

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