Sort

What is sort node in Node-RED?

The Sort node in Node-RED is a function node used to sort either an array of data within a message or a sequence of messages themselves, either in ascending or descending order.

Configuring Sort node

  • Sort:

    • msg.<property>: Specify the message property which contains array data and needs to be sorted.
    • Message sequences: Sort a sequence of messages. Selecting this sort node relies on the received messages to have msg.parts set. The split node generates this property, but it can be manually created. It has the following properties:
      • id: An identifier for the group of messages.
      • index: The position within the group.
      • count: The total number of messages in the group.
  • Key:

    • Element value: Selecting this will sort the array based on the value of the element.
    • Expression: Specify a JSONata expression for sorting property values.
  • Order:

    • Ascending: Sorts the data in ascending order.
    • Descending: Sorts the data in descending order.
  • As numbers: When checked, treats the values as numbers for numerical ordering.

Usecases

  • Data Processing: Sort nodes are often used in data processing pipelines where you need to arrange data in a particular order before further processing. For example, sorting a list of items alphabetically or numerically before performing analysis or displaying the data.

  • Message Sequences: When dealing with message sequences, the Sort node can be used to order messages based on specific criteria. This is particularly useful when working with data streams where the order of messages matters, such as processing time-series data or event streams.

  • Dashboard Displays: In dashboard applications created with Node-RED, the Sort node can help arrange data in a visually appealing manner. For instance, sorting a list of items alphabetically before displaying them in a dropdown menu or a table on a web interface.

  • Data Aggregation: When aggregating data from multiple sources, the Sort node can be used to organize the aggregated data in a meaningful way. This could involve sorting data based on timestamps, numerical values, or any other relevant criteria.

  • Filtering and Filtering: By sorting data, it becomes easier to filter out specific elements or identify outliers. For example, you might use the Sort node to identify the highest or lowest values in a dataset, or to filter out items that fall above or below a certain threshold.

Examples

  1. In the example below, the Sort node sorts the array of numbers and array of the English alphabet in ascending order.
  1. In the example below, the Sort node sorts the message sequences in ascending order based on the msg.payload.quantity property of each message.

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 messages
  • index - the position within the group
  • count - 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.

  • nodeMessageBufferMaxLength property set in settings.js.