# Function for sorting output by input

**URL:** https://discourse.nodered.org/t/function-for-sorting-output-by-input/43069
**Category:** General
**Created:** [23 March 2021 09:17 UTC](https://discourse.nodered.org/t/function-for-sorting-output-by-input/43069 "2021-03-23T09:17:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mocky](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mocky/32/69796_2.png) [@mocky](https://discourse.nodered.org/u/mocky)
#### Post date: [23 March 2021 09:17 UTC](https://discourse.nodered.org/t/function-for-sorting-output-by-input/43069/1 "2021-03-23T09:17:12Z")

</div>

\*\*Good day  
We are starting with node red and I would like to ask if anyone could advise me.  
The point is that I would need a function that would sort my input.  
Now it does to me that when I send any input 1-4, the output is always sent to all 4 outputs.

I would need that when I send input 2, the output will only be on output 2 (Output 1,3,4, they don't get any signal).

I will be happy for any advice.  
Thank you\*\*

 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/26402b919c80cf3def520a61439ccc74e9438ad2.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [23 March 2021 09:22 UTC](https://discourse.nodered.org/t/function-for-sorting-output-by-input/43069/2 "2021-03-23T09:22:18Z")

</div>

> [@mocky](#):
>
> I would need that when I send input 2, the output will only be on output 2 (Output 1,3,4, they don't get any signal).

```javascript

  return [
     msg.payload == 1 ? msg : null,
     msg.payload == 2 ? msg : null,
     msg.payload == 3 ? msg : null,
     msg.payload == 4 ? msg : null
   ];

```

NOTE: as you send in 1 or 2 or 3 or 4, there is no real need to create a new msg { payload:"1" } - just send the original msg.

NOTE: 2 why not just use a switch node? (is this an academic test?)

---

<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: [22 May 2021 09:22 UTC](https://discourse.nodered.org/t/function-for-sorting-output-by-input/43069/3 "2021-05-22T09:22:25Z")

</div>

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