Sorting in "Edit fields" node returns null

Describe the problem/error/question

sorting arrays in “Edit Fields” node returns “null” after upgrade to n8n version 2.36.7, it should return the sorted array

  • Edit Fields (Set) node version 3.4 (Latest version: 3.5)
  • it works in version 2.27.4

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

[
{
“sorted”: null
}
]

Information on your n8n setup

  • n8n version: 2.36.7
  • Database (default: SQLite): postgress
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: ubuntu

Hey @jxyz, while you wait for a response, here are some things that might help:

Suggested resources

Automatically matched to your question.

Docs:

Forum:

@hubschrauber, @nguyenthieutoan, @achamm - you’ve helped with similar issues before, can you take a look?

Automatically suggested by n8n’s community bot. It’s a pilot - please share feedback here.

Hi @jxyz

Try this, this is working

Hi @jxyz
sort() is the only thing in that expression that mutates its receiver instead of returning a new array, and mutating $json in place from an expression is the most probable cause of the null. Sort a copy:
{{ [...$json.arr].sort((a, b) => b - a) }}
If the copy returns [3, 2, 1] while the original still returns null, the break is in mutating the item data rather than in sort() itself, which is worth a bug report since the docs still show that exact compare function.

Before you go down the sort() mutation rabbit hole - check the Type dropdown on that “sorted” field first. Edit Fields v3 has this per-field type selector (String/Number/Array/Object) that the old Set node didn’t have. If it’s set to anything other than Array, n8n tries to force the result into that type and just goes null instead of throwing an error. Preview still looks fine because it skips that coercion step.

Hey @jxyz, both worth ruling out, but I don’t think either is it. I rebuilt your setup on 3.4 and 3.5: sorting in place returns the sorted array fine on both, and a mismatched Type gives you a coerced string rather than null. The only thing that produces exactly {“sorted”: null} is the expression resolving to undefined while the field type is Array.

So sort() isn’t what broke, the reference is. Drop the .sort() and output the raw path on its own, still typed Array. If that comes back null too, your array isn’t where it was on 2.27.4 and the real change is upstream of this node rather than in the sort.

@Anshul_Namdev Thanks, that workaround works, I also found a bug report related to this:

@jxyz glad it helped, can you consider marking that as a solution :smiley:

works again in 2.38.0