How to sort array in ascending order?

Describe the problem/error/question

I have an array and I want to sort the list in ascending order. How can I do this? I want the field “filename” to be in ascending order. All other fields should follow the order if “filename” is sorted

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • **n8n version:1.89.2
  • **Database (default: SQLite):default
  • **n8n EXECUTIONS_PROCESS setting (default: own, main):default
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):npm
  • **Operating system:Windows Server 2022
1 Like

Ehy!
Does the sort node you entered in the workflow not work?

Alternatively, do not use a simple sort but a via code.

Hi @Ruriko

This is a nice one to be honest :smiley:

I’ve attached a working example for your use case, Please give it a try and let me know how it goes!

The key is to split the arrays into individual items so you can use the Sort node. To keep the other fields in sync, concatenate them (link, filename, directory) before splitting; then sort the combined items and finally aggregate them back into the original array structure, now sorted.

I hope the example explains it more clearly:


If this answers your question, Kindly mark the reply as the solution :white_check_mark: so others with the same issue can find it easily :pray:t2:

2 Likes

Another try for sorting in expression
But this will only sort filename only.

@darrell_tw There is a tricky part: syncing the other fields after sorting by filename,
Your solution only sorts filename, without syncing the other fields…

1 Like

Yeah didn’t see the requirement. I thought it’s only sorting filename.

Will change the context!

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