N8n Sort-Node does not sort for me

Hallo n8n-Community!

Because I couldn’t find a way to sort the output in the HTTP request, I wanted to use the Sort node to sort it in the correct order.

I actually wanted to sort it by the value of the partNumber (the 7th value in the object), but unfortunately that didn’t work either. So I wanted to sort it at least by the ID, and unfortunately that didn’t work for me either.

Does anyone have any idea what’s going wrong with this?

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.95.3
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ???
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Debian

You get a response with this structure:

{
"objects": [
{ "id": "45404316", "partNumber": "RT-MG-10000080", ... },
{ "id": "45403328", "partNumber": "RT-MG-10000070", ... }
]
}

n8n sees this as a single item, so it can’t sort that array directly internally.

We continue with a Code Node: Expand to Multiple Items
Add this node just after the HTTP Request to split the objects array into individual items:

// We take the array from the ‘objects’ property of the first item
return items[0].json.objects.map(obj => {
return { json: obj };
});
This transforms the response into a stream of several individual items, one for each object in the array.

and then a Sort Node
Now that you have multiple items in the flow, you can use the Sort node like this:

  • Type: Simple
  • Field to Sort By: id or partNumber (depending on what you need)
  • Order: Ascending or Descending

Workflow Structure
Http Request → Code Node (Expand to Multiple Items) → Sort Node

2 Likes

Thank you, so it can’t be an object anymore. That was the solution! :wink:

1 Like

It is somewhat confusing initially to handle arrays and json to be processed, the easiest way to do this is with node code, in my case this way I work more efficiently, if you have the complete idea of ​​the flow you are looking for I could help you, but from node code the flow is more controlled

1 Like

{{ $json.objects[0].price + $json.objects[0].price/100*$json.objects[0].part.taxRate }}

Thank you very much. I need to calculate a value in JSON, but adding it doesn’t seem to work. Unfortunately, the values ​​are just written together instead of being added together.

The result is: 2800532 instead of 3332

Do you happen to have any idea how the addition works?

1 Like

This indicates that the addition is occurring as a string concatenation, not a mathematical operation. This happens when one or more operands are treated as strings instead of numbers.

Make sure all values ​​used in the operation are explicitly converted to numbers, like this:

{{ Number($json.objects[0].price) + (Number($json.objects[0].price) / 100) * Number($json.objects[0].part.taxRate) }}

This ensures that n8n interprets the expression as a numeric operation, not a text concatenation.

In JavaScript, when the + operator is used between a string and a number, the number is converted to a string, and a concatenation is performed instead of a numeric addition. For example:

"1000" + 200 // Result: "1000200"

To avoid this behavior and ensure that numeric operations are performed, it is essential to explicitly convert strings to numbers using the Number() function.

1 Like