Sorting Data

The sort() is the primary method of sorting event data:

Top or Bottom N Values

To get the 'top' number of events, the sort() has a limit parameter to limit the output; with the addition of the order parameter this can be used to sort and order from the highest to the lowest or vice versa. For example, with this input event set (from a groupBy()):

host_count
BACKUP015
DC017
DEV-APP013
DEV-DB013
DEV-TEST014
DEV-US-APP023
DEV-WEB016
DHCP017
DNS012
LON-SRV017
MAIL015
NYC-SRV016
PRINT015
PROD-APP025
PROD-DB016
PROD-EMEA-WEB013
PROD-FILE018
PROD-SQL011
PROD-WEB014
QA-EU-TEST013
STG-APAC-DB013
SYD-SRV014
TYO-SRV017

The top 5 (by count):

logscale
groupby(host)
| sort(_count,order=desc,limit=5)

Produces

host_count
PROD-FILE018
DC017
DHCP017
LON-SRV017
TYO-SRV017

The bottom 5 (by count):

logscale
groupby(host)
| sort(_count,order=asc,limit=5)
host_count
PROD-SQL011
DNS012
DEV-APP013
DEV-DB013
DEV-US-APP023

If the dataset is large, a quicker approximate value can be obtained using top().

First and Last by Timestamp

To get the same information ordered by the timestamp data, use the tail() and head() functions. These return the last or first events according to the timestamp.

For example, to get the latest event:

logscale
host = "DEV-DB01"
| tail(1)

Might return:

@timestamp#hostname
2025-12-17 06:58:41.799DEV-DB01

Alternative, if you are looking for only one value, use the selectFromMin() or selectFromMax() functions and add @database as one of the selected values:

logscale
selectFromMin(@timestamp, include=[#hostname, @timestamp])

Gets the oldest value.