# Struggling with sort in a function node

**URL:** <https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617>\
**Category:** General\
**Created:** [14 May 2022 09:21 UTC](https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617 "2022-05-14T09:21:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [14 May 2022 09:21 UTC](https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617/1 "2022-05-14T09:21:18Z")

</div>

I must be missing something obvious here. I've dumbed my problem down to a simple function node containing the following:

```auto
const values = [
    { name: "a", size: 1 },
    { name: "b", size: 4 },
    { name: "c", size: 2 }
    ];
    
const sorted = values.sort((x, y) => y.size > x.size);
msg.payload = sorted;

return msg;

```

When I run it, the payload is unsorted. I'm expecting to get 'b' first, because it has the largest size However if I run the same code on an online JS compiler, it works as expected. If I change the sort to log what it's doing, I get the expected values:

```auto
const sorted = values.sort((x, y) => {
    var result = y.size > x.size;
    node.warn(y.name+ " "+y.size+" "+x.size+" "+result);
    return result;
});

```

This gives me:

```auto
a 1 4 false
b 4 2 true

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [14 May 2022 09:28 UTC](https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617/2 "2022-05-14T09:28:36Z")

</div>

google is your friend [Sort an Array of Objects by Values of Object's Properites in JavaScript](https://www.javascripttutorial.net/array/javascript-sort-an-array-of-objects/)

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [14 May 2022 09:30 UTC](https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617/3 "2022-05-14T09:30:01Z")

</div>

You think very little of me to assume I have not Googled the crap out of it. The link you provided contains the same code I'm using.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [14 May 2022 09:30 UTC](https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617/4 "2022-05-14T09:30:54Z")

</div>

> [@michaelblight](#):
>
> `const sorted = values.sort((x, y) => y.size > x.size);`

Could you try instread:

```auto
const sorted = values.sort((x, y) => y.size - x.size);

```

The reasoning, the sort function sorts:

> The default sort order is ascending, built upon converting the elements into strings, **then comparing their sequences of UTF-16 code units values.**

ie. it is not comparing the property values (in the way as one would expect).

---

<div class="post-metadata">

**Author:** ![michaelblight](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/michaelblight/32/445_2.png) [@michaelblight](https://discourse.nodered.org/u/michaelblight)\
**Post date:** [14 May 2022 09:35 UTC](https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617/5 "2022-05-14T09:35:28Z")

</div>

That did it! Thanks, I knew it was something dumb I was doing.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [14 May 2022 09:41 UTC](https://discourse.nodered.org/t/struggling-with-sort-in-a-function-node/62617/6 "2022-05-14T09:41:21Z")

</div>

can you click mark as solution ?
