Sort an Array in n8n

Difficulty
1/10
Tags
EssentialsExpressionsArraysSorting

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

  1. 1Copy the exercise below
  2. 2Paste into your n8n editor (Ctrl+V)
  3. 3Solve it — use hints if you get stuck
  4. 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.

Hints

  1. Add an Edit Fields (Set) node after the existing node
  2. Create new fields for each sorted array (numbers_sorted, names_sorted, dates_sorted)
  3. Use the .sort() method on each array inside an expression (e.g., $json.numbers.sort())
  4. Bonus hint: To reverse the order, chain .reverse() after .sort() like this: .sort().reverse()

Common mistake: You might think to use the Sort node—but it won't work here! The Sort node sorts workflow items (multiple rows of data flowing through your workflow), not values inside an array field. Think of it this way: if you had 100 customer records and wanted to sort them by name, you'd use the Sort node. But if you have a single field containing a list like ["Charlie", "Alice", "Bob"], you need the .sort() expression method instead.

Note: .sort() sorts alphabetically by default, which works perfectly for strings, names, and ISO dates (YYYY-MM-DD format).

Explanation

Why this solution works:

You have three unsorted arrays and need to produce sorted versions of each. The solution uses an Edit Fields (Set) node to create new fields containing the sorted results.

How it solves the problem:

  1. The Edit Fields node creates new data. Instead of modifying the existing arrays, we create new fields (numbers_sorted, names_sorted, dates_sorted). This is a common pattern in n8n: take data in, transform it, output new data.
  2. The expression $json.numbers.sort() does three things:
    • $json refers to the data coming from the previous node
    • .numbers accesses the array field we want to sort
    • .sort() is a JavaScript method that reorders the array alphabetically
  3. Why .sort() works for all three arrays:
    • Numbers as strings: ['1', '4', '2'] sorts to ['1', '2', '4'] because alphabetical order matches numerical order for single digits
    • Names: ['Zoe', 'Mike', 'Anna'] sorts to ['Anna', 'Mike', 'Zoe'] alphabetically
    • ISO dates: ['2024-06-15', '1850-03-01', '1999-12-31'] sorts to ['1850-03-01', '1999-12-31', '2024-06-15'] because the YYYY-MM-DD format is designed to sort alphabetically—even across centuries!
  4. The bonus .sort().reverse(): Chaining .reverse() after .sort() first sorts the array (A to Z), then flips it (Z to A). This gives us ['Zoe', 'Mike', 'Anna'].

Watch out: If you had numbers like [1, 10, 2], alphabetical sorting would give [1, 10, 2] (because "10" comes before "2" alphabetically). For true numeric sorting, you would use .sort((a, b) => a - b).

Copy the Solution

Paste into n8n with Ctrl+V to compare with your approach.

How was this lesson?

Get the solution workflow

Create a free account to copy solutions into your n8n editor.

Related Content

Continue your learning journey with these related lessons and guides: