SORT

In a data query, you can use the SORT operator to aggregate and order elements in a meaningful way. It does not impact visualizations.

It is useful for the query operators and aggregation functions for which the behavior depends on a logical order:

This page discusses:

See Also
Data Query Views
Expression Language
Defining Data Queries

RESULT SET Order VS SORT Operator

By default, the output of a data query displays in no particular order. To order elements in a meaningful way, you have two options:

  • Either Run the data query and sort the order in a View.
  • Or add a SORT operator beforehand in the processing graph of your data query.

If you define an order at the data query View level, it has the precedence over the SORT operator.

General Parameters

Parameter Description
Label

Specify the label to display below the operator type in the data query.

Description

Add a description to display as tooltip on hover over the operator icon.

Tip: It is useful to explain what the operator does, especially when you share the data query with other users.
Operator input variable Specify the name to give to the variable that contains the input data. The variable is then available when you define expressions.

Sorter Criteria Parameters

Whenever you set an order in a data query View or use the SORT operator, elements are ordered according to a list of sorters.

Sorter type Description
Value Sorter

Works as in SQL and asks for 3 elements:

  • A sort expression to get the comparable value.
  • The sort order: ASC/DESC.
  • Handle NULL elements: LAST/FIRST.

For example, to sort a list of tickets by rising id:

expression=ticket.id, orderAsc=true, nullsLast=true
Explicit Sorter

Takes an expression that extracts a value and specifies this value order.

For example, to sort tickets by status ("new" < "qa" < "closed"):

expression=ticket.status, order=["new","qa","closed"],
        orderAsc=true, nullsLast=true
Tree Sorter Hierarchical sort between lists, for example, [0, 1] < [0, 2] < [1, 0]. The sorting criteria following the Tree Sorter allow you to choose the sorting for the child nodes of the same parent node.

The configuration asks for 3 elements:

  • A sort expression to get the comparable value.
  • The sort order: ASC/DESC.
  • Handle NULL elements: LAST/FIRST.

To sort by multiple attributes, specify a list of sorters. You can sort by an attribute in ascending order and then by another attribute in descending order. The second sort applies to the attributes retained by the first sort.

For example, to sort tickets by status and then by descending ticket id:

  • Add an explicit sorter:
    expression=ticket.status, order=["new","qa","closed"], 
                orderAsc=true, nullsLast=true
  • Then add a value sorter:
    expression=ticket.id, orderAsc=false, nullsLast=true

Input

There is one input. There are no constraints on the element type or on the number of elements in the stream.

Output

There is one output. The output stream counts as many elements as the input stream. It does not modify the elements. Only the order in the stream might change.

Example

In this example, we sort the employees by salary.

The output stream is ordered.



Multiple Sorters

Important: With a SORT operator, the result order can change when you run the data query again (if several persons have the same salary in the example above). The data query View offers a stable result order by choosing an arbitrary order that does not change when you run the data query again.