Sort
Sorts an array or a sequence of messages.
Where and why do we use the Sort node?
The Sort node arranges data in ascending or descending order. You can sort either an array within a message payload or a sequence of messages based on their properties. This is essential when you need to organize data before displaying it, process items by priority, or find top/bottom values in datasets.
Modes of operation
The Sort node operates in two different modes:
Array Sorting
Sorts an array stored in a message property. The entire array gets arranged based on element values or a JSONata expression. Use this when you have a complete dataset in one message that needs ordering.
Message Sequence Sorting
Sorts a sequence of messages that have a msg.parts property. Messages need these fields in msg.parts:
- id - identifier for the message group
- index - position within the group
- count - total messages in the group
The Split node automatically creates msg.parts, but you can set it manually if needed. Use this mode when processing streams of individual messages that need to be reordered based on their properties.
How the node handles messages
The Sort node buffers messages internally when working with message sequences. For array sorting, it processes the array immediately and outputs the sorted result. For message sequences, it collects all messages in the sequence before sorting and releasing them in the new order.
When sorting, you can specify:
- Element value - Sorts based on the element's value directly
- Expression - Uses a JSONata expression to extract the sort value from complex objects
The sort direction can be:
- Ascending - Smallest to largest (A to Z)
- Descending - Largest to smallest (Z to A)
Enable As numbers to sort numerically instead of alphabetically. Without this, "10" comes before "2" because it's treated as text.
Examples
Sorting arrays
This example sorts numbers and letters in ascending order. The arrays get arranged from smallest to largest, or A to Z.
Sorting message sequences
Here the Sort node arranges a sequence of messages in descending order by the quantity property. The Split node breaks the array into individual messages, each gets sorted by its quantity value, and higher quantities come first.
Node Documentation
A function that sorts message property or a sequence of messages.
When configured to sort message property, the node sorts array data pointed to by specified message property.
When configured to sort a sequence of messages, it will reorder the messages.
The sorting order can be:
- ascending,
- descending.
For numbers, numerical ordering can be specified by a checkbox.
Sort key can be element value or JSONata expression for sorting property value, or message property or JSONata expression for sorting a message sequence.
When sorting a message sequence, the sort node relies on the received messages to have msg.parts set. The split node generates this property, but can be manually created. It has the following properties:
id- an identifier for the group of messagesindex- the position within the groupcount- the total number of messages in the group
Note: This node internally keeps messages for its operation. In order to prevent unexpected memory usage, maximum number of messages kept can be specified. Default is no limit on number of messages.
nodeMessageBufferMaxLengthproperty set in settings.js.