# How to find the two lowest values of five values

**URL:** <https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473>\
**Category:** General\
**Tags:** function-node\
**Created:** [1 October 2022 23:19 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473 "2022-10-01T23:19:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [1 October 2022 23:19 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/1 "2022-10-01T23:19:28Z")

</div>

Hi guys,

I have multiple msg.payloads and i want to pick out the two lowest values out for these.  
I'm aware of the $min(payload) but this will only pick out the lowest value, I also want the second lowest value.

The use of the function will be to pick out the two lowest price hours and start an action with these values

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [1 October 2022 23:24 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/2 "2022-10-01T23:24:21Z")

</div>

I also have these input values as 'flow.get' values  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/591c37aba4c7ab816752dab4577d8fff04d8bceb.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 October 2022 00:44 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/3 "2022-10-02T00:44:42Z")

</div>

If you put your flow variables into an array you can sort it. Then the two lowest values will be in array[0] and array[1]:

```auto
[
    {
        "id": "839f8962b0f806b1",
        "type": "function",
        "z": "9372feccca70ce91",
        "name": "function 7",
        "func": "flow.set('h01', 0.75);\nflow.set('h02', 0.82);\nflow.set('h03', 0.72);\nflow.set('h04', 0.75);\nflow.set('h05', 0.91);\n\nvar myarray = [['h01', flow.get('h01')], ['h02', flow.get('h02')], ['h03', flow.get('h03')], ['h04', flow.get('h04')], ['h05', flow.get('h05')]];\nmsg.payload = myarray.sort((a,b) => a[1] - b[1])\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 120,
        "wires": [
            [
                "8359dc36427d89e9"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [2 October 2022 06:53 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/4 "2022-10-02T06:53:37Z")

</div>

> [@jbudd](#):
>
> ```auto
> [
> {
> "id": "839f8962b0f806b1",
> "type": "function",
> "z": "9372feccca70ce91",
> "name": "function 7",
> "func": "flow.set('h01', 0.75);\nflow.set('h02', 0.82);\nflow.set('h03', 0.72);\nflow.set('h04', 0.75);\nflow.set('h05', 0.91);\n\nvar myarray = [['h01', flow.get('h01')], ['h02', flow.get('h02')], ['h03', flow.get('h03')], ['h04', flow.get('h04')], ['h05', flow.get('h05')]];\nmsg.payload = myarray.sort((a,b) => a[1] - b[1])\nreturn msg;",
> "outputs": 1,
> "noerr": 0,
> "initialize": "",
> "finalize": "",
> "libs": [],
> "x": 420,
> "y": 120,
> "wires": [
> [
> "8359dc36427d89e9"
> ]
> ]
> }
> ]
> 
> ```

Great! Thank you 🙂

---

<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:** [2 October 2022 07:49 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/5 "2022-10-02T07:49:20Z")

</div>

JUst for info, the context can be fetched as an array.

```auto
let output = flow.get(["h01","h02","h03","h04","h05"]).sort((a,b) => a - b);
msg.payload = output; //msg.payload[0] is lowest msg.payload[1] is next lowest etc
return msg;

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 October 2022 09:03 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/6 "2022-10-02T09:03:42Z")

</div>

That's much nicer code than my back of an envelope attempt @E1cid but although it sorts the values, it loses the association with the hour number.

---

<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:** [2 October 2022 09:15 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/7 "2022-10-02T09:15:35Z")

</div>

If the OP wants association then

```auto
const flow_var_keys = ["h01","h02","h03","h04","h05"];
let output = flow.get(flow_var_keys).
    map((value, index) => { return {"flow_var": flow_var_keys[index], value}}).
    sort((a,b) => a.value - b.value);
msg.payload = output;
return msg;

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [2 October 2022 11:02 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/8 "2022-10-02T11:02:27Z")

</div>

Sometimes some values are exact same. And most of the times it doesn't really matter. But sometimes it does.  
Just saying cos most of the times that little edge case isn't even considered.

---

<div class="post-metadata">

**Author:** ![PowerZone21](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/powerzone21/32/67873_2.png) [@PowerZone21](https://discourse.nodered.org/u/PowerZone21)\
**Post date:** [7 October 2022 23:05 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/9 "2022-10-07T23:05:48Z")

</div>

Thank you all for your examples, it helped me!

---

<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:** [6 December 2022 23:05 UTC](https://discourse.nodered.org/t/how-to-find-the-two-lowest-values-of-five-values/68473/10 "2022-12-06T23:05:48Z")

</div>

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