Sort Items with the Sort Node in n8n

Difficulty
1/10
Tags
EssentialsSortHTTP RequestSplit OutJSONArraysSorting

Data rarely arrives in the order you need. Product lists might come unsorted from an API, customer records might be in random order, or you might need to find the highest-rated or cheapest items first.

The Sort node reorders items based on field values. It offers three modes: Simple (sort by fields), Random (shuffle into random order), and Code (custom JavaScript logic). In this challenge, we will focus on Simple mode - the most common way to sort data.

With Simple mode, you can sort by one field or multiple fields, in ascending or descending order. When sorting by multiple fields, the first field takes priority, and subsequent fields break ties.

What you'll practice:

  • Using the Sort node in Simple mode
  • Sorting by multiple fields with priority
  • Understanding ascending vs descending order

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

Fetch products from https://dummyjson.com/products?limit=12 and sort them by:

  1. Category (alphabetically, A to Z)
  2. Price (lowest to highest, within each category)

Example product structure:

{
  "id": 1,
  "title": "Essence Mascara Lash Princess",
  "category": "beauty",
  "price": 9.99,
  "rating": 4.94
}

Expected result: Products grouped by category in alphabetical order, with the cheapest products first within each category.

Hints

  1. Add a Manual Trigger node to start the workflow
  2. Add an HTTP Request node to fetch from the URL above
  3. The API returns a products array, so add a Split Out node with Field To Split Out set to products
  4. Add a Sort node after Split Out
  5. Set Type to "Simple"
  6. Click Add Field To Sort By and enter category as the field name, with Ascending order
  7. Click Add Field To Sort By again and enter price as the field name, with Ascending order

Tip: The order you add fields matters. The first field is the primary sort, and the second field only affects items that have the same value in the first field.

Explanation

Why we need Sort:

The Sort node lets you control the order of items flowing through your workflow. This is essential when you need to present data in a specific order, find top or bottom values, or group related items together before further processing.

Sort types:

The Sort node offers three modes:

  • Simple: Sort by one or more fields in ascending or descending order (what we use here)
  • Random: Shuffle items into a random order (useful for randomizing selections)
  • Code: Write custom JavaScript for complex sorting logic

How multi-field sorting works:

When you sort by multiple fields, the Sort node uses the first field as the primary sort key. Items are ordered by that field first. Then, for any items that have the same value in the first field, the second field determines their relative order.

How the solution works:

  1. Manual Trigger: Starts the workflow
  2. HTTP Request: Fetches 12 products from the API (returns 1 item with a products array)
  3. Split Out: Set Field To Split Out to products to convert the array into 12 separate items
  4. Sort: Configured with two sort fields - category (ascending) first, then price (ascending)

The result:

Products are now organized with all "beauty" products first (sorted by price), then "fragrances" (sorted by price), then "furniture" (sorted by price), and so on. This makes it easy to browse products by category while seeing the most affordable options first.

Copy the Solution

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

Login to see the exercise

Create an account to access challenges and track your progress.

Log in to see exercise