# Unexpected Results from Item Lists --\> Sort

**URL:** <https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285>\
**Category:** Questions\
**Tags:** data-transformation, google-analytics, item-lists\
**Created:** [November 30, 2022, 6:25am UTC](https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285 "2022-11-30T06:25:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmac](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mmac](https://community.n8n.io/u/mmac)\
**Post date:** [November 30, 2022, 6:25am UTC](https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285/1 "2022-11-30T06:25:57Z")

</div>

When using Item Lists —\> Sort, the items are not being sorted by their numbers as expected:

 ![Screen Shot 2022-11-29 at 10.05.40 PM](https://europe1.discourse-cdn.com/n8n/original/3X/4/2/42e29dd454a13e263508a93e0392feef8bfe6c75.png)

I think maybe the numbers are a string, and they need to be an integer, to sort properly?

What’s odd though is I do have the previous node, Google Analytics, set to return the “total” field as an integer:

 ![Screen Shot 2022-11-29 at 10.09.56 PM](https://europe1.discourse-cdn.com/n8n/original/3X/a/3/a3d9bca34f7ee669908a4b13f70d2dfff21fc8b0.png)

All that said, Google Analytics API does have an “orderBys” option, but I can’t find it within the Google Analytics setting in n8n. If I could use the built-in Google Analytics “orderBys” I could bypass this step in n8n entirely.

So, my questions are:

1. How to use the Google Analytics “orderBys” within the n8n Google Analytics node
2. If Google Analytics is set to return a value as a integer, why is it being treated as a string by the Item Lists?
3. How to convert the string to an integer so I can sort the data by the “total” field?

## Workflow:

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [November 30, 2022, 3:58pm UTC](https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285/2 "2022-11-30T15:58:53Z")

</div>

Hi @mmac, I think you might be spot on here, this very much looks like numbers formatted as strings. I don’t have a Google Analytics account to check this firsthand, but you could verify this by switching to the JSON view.

Integers wouldn’t have quotes around them:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/8/f/8f248c7484feb8a1532bb00244c606f51b53d213.png)

But strings would:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/2/b/2bf396b2ac2d932f6485a3e2ef56226103280a97.png)

If you need to convert a strings into integers, you could use [parseInt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) in an [expression](https://docs.n8n.io/code-examples/expressions/) (such as `{{ parseInt($json.total) }}` where `total` is your field name).

Here’s an example workflow showing the idea and the difference between strings and integers:

Numbers:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/f/7/f734389072eb4162bc1dfe62b4fdc00274f82347.png)

Strings:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/b/9/b9be88ed367eb88150e5b39f65545d30735b59e7.png)

Hope this helps! Let know if you have any questions on this.

---

<div class="post-metadata">

**Author:** ![mmac](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mmac](https://community.n8n.io/u/mmac)\
**Post date:** [November 30, 2022, 6:40pm UTC](https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285/3 "2022-11-30T18:40:22Z")

</div>

> [@MutedJam](#):
>
> {{ parseInt($json.total) }}

Thank you! The fields are coming through with quotes, so as strings.

I’m not able to get the parseInt to work though.

If I do `{{parseInt($node["Google Analytics"].json["total"])}}`  
or `{{ parseInt($json.total) }}` or, without the parseInt just `{{$node["Google Analytics"].json["total"]}}` it gives the error, “ERROR: Couldn’t find the field ‘286’ in the input data.” (Sub “286” for whatever number is in the “total” field).

That’s why I was using the “total” field as “Fixed” rather than as an expression in the Item Lists node, because it didn’t work as an expression.

Here’s some sample data from Google Analytics when the “Simplify” option is on, maybe it’s formatted weird?

```auto

[
{
"ga:pagePath":
"/",
"total":
"286",
"ga:pageTitle":
"My Site"
},
{
"ga:pagePath":
"/?post_type=post",
"total":
"3",
"ga:pageTitle":
"My Site Another Page"
},
{
"ga:pagePath":
"/about-us,
"total":
"1",
"ga:pageTitle":
"My Site About"
},
{
"ga:pagePath":
"/contact,
"total":
"1",
"ga:pageTitle":
"My Site Contact"
}
]

```

And if I don’t have “Simplify” on in the Google Analytics node, it comes through like:

````auto
[
  {
    "columnHeader": {
      "dimensions": [
        "ga:pagePath",
        "ga:pageTitle"
      ],
      "metricHeader": {
        "metricHeaderEntries": [
          {
            "name": "total",
            "type": "INTEGER"
          }
        ]
      }
    },
    "data": {
      "rows": [
        {
          "dimensions": [
            "/",
            "My Site"
          ],
          "metrics": [
            {
              "values": [
                "293"
              ]
            }
          ]
        },
        {
          "dimensions": [
            "/about",
            "My Site About"
          ],
          "metrics": [
            {
              "values": [
                "3"
              ]
            }
          ]
        },
        {
          "dimensions": [
            "/contact,
            "My Site Contact"
          ],
          "metrics": [
            {
              "values": [
                "1"
              ]
            }
          ]
        },
        {
          "dimensions": [
            "/another-page",
            "My Site Another Page"
          ],
          "metrics": [
            {
              "values": [
                "1"
              ]
            }
          ]
        }
      ],
      "totals": [
        {
          "values": [
            "1003"
          ]
        }
      ],
      "rowCount": 189,
      "minimums": [
        {
          "values": [
            "1"
          ]
        }
      ],
      "maximums": [
        {
          "values": [
            "293"
          ]
        }
      ]
    }
  }
]```
````

---

<div class="post-metadata">

**Author:** ![MutedJam](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/mutedjam/32/70851_2.png) [@MutedJam](https://community.n8n.io/u/MutedJam)\
**Post date:** [December 1, 2022, 3:49pm UTC](https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285/4 "2022-12-01T15:49:16Z")

</div>

Sorry to hear you’re still having trouble here @mmac. I used your example data and it converts fine for me in a workflow like this:

This was the result:

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/f/9/f99adfa79def48ae522a4f5d9dcc4320269cb3d9.png)

Perhaps you can share an example workflow using which your problem can be reproduced? Simply select the relevant nodes on your n8n canvas and paste the data here on the forum.

In addition, can you confirm which version of n8n exactly you are currently using?

---

<div class="post-metadata">

**Author:** ![mmac](https://avatars.discourse-cdn.com/v4/letter/m/76d3ee/32.png) [@mmac](https://community.n8n.io/u/mmac)\
**Post date:** [December 5, 2022, 7:06pm UTC](https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285/5 "2022-12-05T19:06:42Z")

</div>

I can’t for the life of me figure out what I was doing wrong before, as it’s working for me now as well. Must’ve had a typo somewhere I didn’t notice. Thank you, everything is tip top now.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [December 12, 2022, 7:07pm UTC](https://community.n8n.io/t/unexpected-results-from-item-lists-sort/20285/6 "2022-12-12T19:07:15Z")

</div>

Closed on December 12, 2022, 7:07pm UTC

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