
Module 4 Lesson 5: Sorting & Filtering
Slice and dice. Learn how to use the 'Sort' and 'Filter' nodes to ensure you only process the most relevant data and keep your workflows efficient.
Module 4 Lesson 5: Sorting & Filtering
If you pull 1,000 customers from a database, but you only want to email the ones who spent > $500, you need Filtering. If you want to email the "Most Recent" customers first, you need Sorting.
1. The Filter Node
Think of this as a "Sieve."
- Only pass items where
price > 100. - Only pass items where
category = 'VIP'. - Result: The node outputs a smaller list of items, saving you "API Calls" and "CI Minutes" in the following nodes.
2. The Sort Node
Decide the order of the items.
- Sort by
created_at(Descending) to get the newest items. - Sort by
name(Alphabetical). - Scenario: You want to notify Slack about the 3 Highest Sales of the day. You Sort by
value(Desc) and then go to Step 3.
Visualizing the Process
graph TD
Start[Input] --> Process[Processing]
Process --> Decision{Check}
Decision -->|Success| End[Complete]
Decision -->|Retry| Process
3. The Limit Node (The "Cutter")
Only take the first X items.
- "Give me the top 5 results."
- "Skip the first 10 items (Offset) and give me the next 10."
- This is essential for Pagination (reading massive lists in small chunks).
4. The "No Data" Case
What happens if the Filter node removes EVERYTHING?
- n8n will stop the workflow (by default).
- You can change this behavior in the "Settings" of the node if you want the workflow to continue even with 0 items.
Exercise: The Curator's Task
- Create a mock list of 5 items with
scorefrom 1 to 100. - Filter the list to only include items where
score > 50. - Sort the remaining items from Highest to Lowest score.
- Limit the result to only the Top 1 item.
- Why should you filter as EARLIER as possible in a workflow? (Hint: Cost and Speed).
- Research: What is the "Remove Duplicates" node?
Summary
You have completed Module 4: Working with Data. You now know how to read JSON, manipulate strings, calculate math, handle time, and curate your datasets. You have the "DNA" of an automation expert.
Next Module: Advanced Logic: Module 5: Advanced Logical Flow (IF, Switch, Merge).