Browse docs
On this page

sort command

Sort the result set. Prefix a field with - for descending.

All ObiQL commands

Orders the result rows by one or more fields. Fields sort ascending unless prefixed with -, which sorts descending.

It works on raw events and on the output of stats, timechart, eval and the other stages.

Syntax

ObiQL
| sort [-|+]<field> [, [-|+]<field> ...]

Square brackets mark an optional part, and angle brackets a value you supply.

Arguments

ArgumentRequiredDescription
<field>RequiredA field or result column to sort by. Prefix it with - for descending or + (the default) for ascending. Quote a name that contains a space: | sort -"Total Errors".

Usage

  • A field with a known type sorts as that type: the timestamp by time, and numbers from eval or an aggregation numerically.
  • Other event fields are stored as text, so values that look like numbers sort numerically and come before values that do not, which sort as text. | sort -amount puts 100 ahead of 10 ahead of 9.
  • Rows missing the field sort last in both directions.
  • Without a sort, events come back newest first.
  • sort cannot follow dedup. Put the sort first to choose which row dedup keeps.

Examples

1. Busiest services first

ObiQL
| stats count() by service | sort -count

Sorts the aggregate by its count column, largest first.

2. Slowest requests

ObiQL
service=checkout-api | sort -duration_ms | limit 20

The twenty slowest requests in the time range.

3. Sort by several fields

ObiQL
| stats count() by service, level | sort service, -count

Groups the rows by service alphabetically, then by count within each service.