sort
Order rows by one or more fields, ascending or descending, with an optional row limit.
Use sort to order your results by one or more fields. Choose ascending or descending order and set a row limit when you need only part of the result.
Syntax
... | sort [by] [<N>] [+|-]<field> [, [+|-]<field> ...] [limit=<N>]
... | sort [by] [<N>] <field> [asc|desc] [, <field> [asc|desc] ...] [limit=<N>]| Argument | Default | Description |
|---|---|---|
field | — | The field to sort on. The first field is the primary key; later fields break ties. Separate with commas or spaces. Quote names with spaces. |
- / desc | — | Descending (largest, newest or Z first). |
+ / asc | ascending | Ascending (smallest, oldest or A first). A -/+ prefix wins over an asc/desc suffix. |
N (before the fields) or limit=N | all rows | Keep only the first N rows after sorting. |
by | — | Optional, for readability (sort by -count). |
How it works
- Event searches without
sortcome back newest first (timedescending).sort timeturns them oldest first. - After
stats, sort on the aggregate's output name. Name aggregates withasso you can sort on them:stats sum(traffic.bytes_out) as sent by src_endpoint.ip | sort -sent. A barecountcolumn is namedcount. sort Nandlimit=Nare the same assort … | head N.
Examples
Users with the most failed logons first:
class_uid=3002 status_id=2 | stats count by user.name | sort -countThe five proxy clients that sent the most bytes, using the leading-N form:
metadata.log_source=conduit_proxy | stats sum(traffic.bytes_out) as sent by src_endpoint.ip | sort 5 -sentPrimary and secondary keys, mixing directions:
class_uid=4002 | stats count by http_request.url.hostname, http_response.code | sort http_request.url.hostname, -countSuffix style with a trailing limit:
class_uid=1007 | stats dc(device.hostname) as hosts by process.name | sort by hosts asc limit=20Oldest events first:
metadata.log_source=windows_event | sort time | head 20Notes
sortalso runs after a nano-side stage (lookup,prevalence,inputlookup,resolve_identity,ai,lateral).- stats already orders its output by
countdescending when it has acountcolumn. Addsortwhen you need a different order.