# Help with sorting an object

**URL:** <https://discourse.nodered.org/t/help-with-sorting-an-object/52669>\
**Category:** General\
**Created:** [24 October 2021 17:19 UTC](https://discourse.nodered.org/t/help-with-sorting-an-object/52669 "2021-10-24T17:19:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [24 October 2021 17:19 UTC](https://discourse.nodered.org/t/help-with-sorting-an-object/52669/1 "2021-10-24T17:19:51Z")

</div>

```auto
payload: object
MONITOR-PLUG-11: object
NTP: "41"
Int: "12"
Last: "46m"
OUTSIDE-LIGHTS: object
NTP: "27"
Int: "12"
Last: "14h"
BULB-15: object

```

Hi All,

As a bit of a learning exercise I decided to write a flow to check state of my Chrony time server.

After much trial and error I have taken the output of an exec node, split on new lines, removed spaces and managed to create an object as above. This is all done in one function node, but I'm stuck on the last bit!

How do I go about soring the above object by the key? names - MONITOR-PLUG-11 etc?

Have spent far too long trying to work this out so no longer fun 😉

Cheers

---

<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:** [24 October 2021 18:04 UTC](https://discourse.nodered.org/t/help-with-sorting-an-object/52669/2 "2021-10-24T18:04:57Z")

</div>

> [@Sean-McG](#):
>
> How do I go about soring the above object by the key? names - MONITOR-PLUG-11 etc

The question is why? For viewing purposes? For iterating?

The truth is a lot more difficult to explain but this stack overflow post does it some justice...

> <https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties/30919039#30919039>

Tbh, I am struggling to think of a good reason to sort keys in an object.

---

<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 October 2021 19:28 UTC](https://discourse.nodered.org/t/help-with-sorting-an-object/52669/3 "2021-10-24T19:28:15Z")

</div>

As Steve said you would not normally sort an object, but it is possible if you have reason to do so.

```auto
let obj = msg.payload;
msg.payload = Object.keys(obj).sort().reduce((r, k) => Object.assign(r, {[k]: obj[k]}), {})
return msg;

```

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [24 October 2021 23:25 UTC](https://discourse.nodered.org/t/help-with-sorting-an-object/52669/4 "2021-10-24T23:25:48Z")

</div>

Many thanks both.

As I mentioned I was really just trying to learn a bit more about manipulating objects etc. Turning a console output to a usable object seemed like a good exercise.

I understand comments about the sort order but I knew it could be done, and it is easier to check in a debug window for missing devices that way.

@Steve-Mcl I actually remember seeing that post among many others, but now I have working code its easier to reverse engineer to understand how things work.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [25 October 2021 00:27 UTC](https://discourse.nodered.org/t/help-with-sorting-an-object/52669/5 "2021-10-25T00:27:55Z")

</div>

Example here if anyone is interested.

[https://flows.nodered.org/flow/1c8e6bfab35fb2526b9588d677a7955b](https://flows.nodered.org/flow/1c8e6bfab35fb2526b9588d677a7955b)

---

<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:** [8 November 2021 00:28 UTC](https://discourse.nodered.org/t/help-with-sorting-an-object/52669/6 "2021-11-08T00:28:43Z")

</div>

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