Sort an Array in n8n
An array is simply a list of items. Think of it like a shopping list or a column in a spreadsheet—it holds multiple values in a specific order.
Your boss wants you to create a workflow that sorts several arrays: numbers, names, and dates. Sorting data is a common task in automation—whether you're organizing contacts alphabetically, arranging events by date, or putting numbers in order.
n8n expressions are small code snippets you write directly in node fields. They support full JavaScript, so you can use built-in methods like .sort() to manipulate your data without needing a separate Code node.
When you type a . after your data (like $json.names.), you'll see a list of available methods. These are built-in functions that manipulate your data in different ways. For this challenge, we'll use .sort() to reorder arrays.
What you'll practice:
- Using JavaScript array methods in n8n expressions
- Sorting different types of data (numbers, names, dates)
- Using the Edit Fields (Set) node to transform data before passing it to the next step
Your Task
- 1Copy the exercise below
- 2Paste into your n8n editor (Ctrl+V)
- 3Solve it — use hints if you get stuck
- 4Check the solution when done
Sort each of the three arrays (numbers, names, dates) and output them in new fields:
numbers_sorted→["1", "2", "4"]names_sorted→["Anna", "Mike", "Zoe"]dates_sorted→["1850-03-01", "1999-12-31", "2024-06-15"]
Bonus: Create a names_reversed field that sorts names in reverse order (Z to A) → ["Zoe", "Mike", "Anna"]
Get the exercise workflow
Create a free account to copy exercises into your n8n editor.