Unexpected Results from Item Lists --> Sort

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

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:

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:

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:

But strings would:

If you need to convert a strings into integers, you could use parseInt() in an expression (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:

Strings:

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

2 Likes

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?


[
{
"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:

[
  {
    "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"
          ]
        }
      ]
    }
  }
]```

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:

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?

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.

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