# Sort an array, get the lowest three values and return the labels from those values

**URL:** <https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653>\
**Category:** General\
**Created:** [24 December 2022 08:01 UTC](https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653 "2022-12-24T08:01:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sstolk11](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@sstolk11](https://discourse.nodered.org/u/sstolk11)\
**Post date:** [24 December 2022 08:01 UTC](https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653/1 "2022-12-24T08:01:01Z")

</div>

I've got this array with different labels and always different values.  
I would like to have the three lowest values and return the labels in from of those values.  
I'm searching my \*ss off but JS and other code programming is not my cup of tea.  
I've tried converting it to a CSV in Node-RED and tried to select the MIN value but I get only one value and I would like have three off them.

Is there a simple solution for this? Or is this not possible with this structure of the array?

 ![Array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/1550042ebc47728f27aa6f03c8d03fb9836e6f57.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:** [24 December 2022 08:45 UTC](https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653/2 "2022-12-24T08:45:35Z")

</div>

To return the lowest 3 labels try

```auto
delete msg.payload[0].datum;
let output = Object.entries(msg.payload[0])
output = output.sort((a,b) => a[1]- b[1]).slice(0,3); // sort and select lowest 3
msg.payload = output.map(arr => arr[0]) //return key only
return msg;

```

---

<div class="post-metadata">

**Author:** ![sstolk11](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@sstolk11](https://discourse.nodered.org/u/sstolk11)\
**Post date:** [24 December 2022 09:15 UTC](https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653/3 "2022-12-24T09:15:49Z")

</div>

Well thank you! You are my life saver!  
When I want to sort it for the highest is this the right code?

```auto
delete msg.payload[0].datum;
let output = Object.entries(msg.payload[0])
output = output.sort((b,a) => a[1]- b[1]).slice(0,3); // sort and select lowest 3
msg.payload = output.map(arr => arr[0]) //return key only

```

Thanks a lot

---

<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:** [24 December 2022 09:43 UTC](https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653/4 "2022-12-24T09:43:05Z")

</div>

Have you tested it, it should work

or in original example you can slice(-3) for last 3 which should be the highest.  
It really depends on how you want the 3 keys returned, low to high or high to low.

---

<div class="post-metadata">

**Author:** ![sstolk11](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@sstolk11](https://discourse.nodered.org/u/sstolk11)\
**Post date:** [24 December 2022 13:50 UTC](https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653/5 "2022-12-24T13:50:54Z")

</div>

After some fiddeling around I managerd to get both formulas work:

```auto
let output = Object.entries(msg.payload[0])
output = output.sort((a,b) => a[1]- b[1]).slice(0,3); // sort and select lowest 3
msg.payload = output.map(arr => arr[0]) //return key only

```

```auto
let output = Object.entries(msg.payload[0])
output = output.sort((b,a) => b[1]- a[1]).slice(-3); // sort and select highest 3
msg.payload = output.map(arr => arr[0]) //return key only

```

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:** [7 January 2023 13:51 UTC](https://discourse.nodered.org/t/sort-an-array-get-the-lowest-three-values-and-return-the-labels-from-those-values/72653/6 "2023-01-07T13:51:48Z")

</div>

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