n8n LabAdvanced
Sort an Array of Objects by a Field in n8n
Show products from cheapest to most expensive in a Telegram message.
Live Execution Simulator
Idle
π₯
Incoming Data
βοΈ
n8n Node
π―
Next Step
> Ready. Type your expression below and click "Check Answer" to run the node.
Incoming Payload / Data:
{
"products": [
{ "name": "Pro", "price": 49 },
{ "name": "Lite", "price": 9 },
{ "name": "Team", "price": 99 }
]
}
$json.products.sort((a, b) =>
)
βοΈ Your expression is executed against test data β any correct solution passes. +30 XP
β Solution & explanation
$json.products.sort((a, b) => a.price - b.price)
Output: Lite, Pro, Team
sort() calls the compare function for pairs of items. Returning a.price - b.price is negative when a is cheaper, so it moves first. Without a compare function, numbers are sorted as text (100 before 9).π‘ Pro tip: Reverse the order (high β low) with b.price - a.price.