# Sorting positive and negative digits

**URL:** https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919
**Category:** General
**Created:** [1 July 2021 22:27 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919 "2021-07-01T22:27:02Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![martc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martc/32/44187_2.png) [@martc](https://discourse.nodered.org/u/martc)
#### Post date: [1 July 2021 22:27 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/1 "2021-07-01T22:27:02Z")

</div>

Hi,  
i have mqtt message payload "-1" and "1", i try switch with two output - not working. Both digits going on one or other output... no metter use "==" "\>" "\<" or "otherwise" etc. if use other digits - positive all work fine. i cant change mqtt payload digits, its from rotary encoder array on esp8266, mqtt payload format is "10:2:-1" changing last part - "-1" or "1" . How i can sort on different outputs "1" and "-1"?

---

<div class="post-metadata">

### Author: ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)
#### Post date: [1 July 2021 22:32 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/2 "2021-07-01T22:32:00Z")

</div>

> [@martc](#):
>
> "-1" and "1"

These are strings, not numbers. You would need to ether send them as numbers or convert them to numbers before doing any numerical comparisons

---

<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: [1 July 2021 22:47 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/3 "2021-07-01T22:47:04Z")

</div>

You can convert the property in the switch node using a JSONata expression.  
e.g.

```auto
[{"id":"b15ec1c0.45137","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-1","payloadType":"str","x":150,"y":3980,"wires":[["62b7b58.d85804c"]]},{"id":"62b7b58.d85804c","type":"switch","z":"c74669a0.6a34f8","name":"","property":"$number(payload)","propertyType":"jsonata","rules":[{"t":"lt","v":"0","vt":"num"},{"t":"gt","v":"0","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":330,"y":4020,"wires":[["b2f576f7.07073"],["8e0f836.ea41a8"],["4855cece.93e67"]]},{"id":"a57c140b.edfb1","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":150,"y":4060,"wires":[["62b7b58.d85804c"]]},{"id":"4a4651cd.ab011","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":150,"y":4020,"wires":[["62b7b58.d85804c"]]},{"id":"4855cece.93e67","type":"debug","z":"c74669a0.6a34f8","name":"0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":4060,"wires":[]},{"id":"b2f576f7.07073","type":"debug","z":"c74669a0.6a34f8","name":"-1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":3980,"wires":[]},{"id":"8e0f836.ea41a8","type":"debug","z":"c74669a0.6a34f8","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":4020,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![martc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martc/32/44187_2.png) [@martc](https://discourse.nodered.org/u/martc)
#### Post date: [2 July 2021 11:48 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/4 "2021-07-02T11:48:30Z")

</div>

Work great, just im think as Gunner say, first - string need convert to number. Im split mqtt payload "3:2:-1"

```auto
var str = msg.payload;
var parts = str.split(":");
msg.payload = {
  dir: parseInt(parts[2]),
}
return msg;

```

and get in output only last part "1 or -1", after conecting switch with JSONata expression i got :  
"Invalid JSONata expression: Argument 1 of function "number" does not match function signature".  
This is what i think, string convert to number and only than input to switch?  
P.S. Sorry if my question seems stupid, im try anderstand node red just short time.

---

<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 July 2021 12:22 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/5 "2021-07-02T12:22:50Z")

</div>

As payload is now a object and msg.payload.dir is a number, the switch property would be msg.payload.dir. If it is still set to $number(payload) then an error will be shown as payload is an object, and $number() accepts strings or numbers.

---

<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: [2 July 2021 12:23 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/6 "2021-07-02T12:23:06Z")

</div>

... not great - but if they are strings then you could just use a switch node with "contains" set to look for -

---

<div class="post-metadata">

### Author: ![martc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martc/32/44187_2.png) [@martc](https://discourse.nodered.org/u/martc)
#### Post date: [2 July 2021 13:51 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/7 "2021-07-02T13:51:31Z")

</div>

Perfect as you say. Set in switch property "$number(payload.dir)" and all work now. If i get time, try read some manual of node red. Thanks.

---

<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: [16 July 2021 13:51 UTC](https://discourse.nodered.org/t/sorting-positive-and-negative-digits/47919/8 "2021-07-16T13:51:55Z")

</div>

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