# Sort objects that contain array values?

**URL:** <https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320>\
**Category:** General\
**Created:** [7 November 2021 04:16 UTC](https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320 "2021-11-07T04:16:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [7 November 2021 04:16 UTC](https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320/1 "2021-11-07T04:16:48Z")

</div>

Guys,

I have an object that represents a series of prices and times - as per the screenshot below

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

What i need to do is loop through all of the objects and then sort them based on the price field (lowest to highest)

Any hints on the way to do this ?

Craig

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [7 November 2021 06:18 UTC](https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320/2 "2021-11-07T06:18:37Z")

</div>

With jsonata you should be able to do something like:

```auto
payload.items.$^(price)[[0, -1]]

```

---

<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:** [7 November 2021 08:27 UTC](https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320/3 "2021-11-07T08:27:30Z")

</div>

Alternatively, a function node can do it:

```auto
msg.payload.items.sort( (A,B) => A.price - B.price )
return msg

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [7 November 2021 08:47 UTC](https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320/4 "2021-11-07T08:47:32Z")

</div>

thanks for the hints guys - will give it a whirl and report back

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [7 November 2021 08:53 UTC](https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320/5 "2021-11-07T08:53:42Z")

</div>

Now i know why NR is so good - if you can whip something out that quickly and easily - dropped your code into a function node chained to my previous node and BOOM - exactly what i needed - thanks Nick

Now to study up online and understand what it is doing !

Craig

---

<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:** [21 November 2021 08:53 UTC](https://discourse.nodered.org/t/sort-objects-that-contain-array-values/53320/6 "2021-11-21T08:53:57Z")

</div>

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