The sort() query function is used to sort events based on a given field — or fields. Events can be sorted by multiple fields by setting the field parameter to an array of field names (for example, between square-brackets in a comma-separated list). Likewise, the order and type of each field can be specified by setting the order and type parameters to arrays.

ParameterTypeRequiredDefault ValueDescription
field[a]array of stringsoptional[b]   The fields by which to sort events.
limitintegeroptional[b] 200 The argument given to this parameter determines the limit on the number of events included in the result of the function. The default argument is default. The maximum is controlled by the StateRowLimit dynamic configuration, which is StateRowLimit by default. If the argument is max (limit=max), then the value of StateRowLimit is used.
orderarray of stringsoptional[b] desc The order in which to sort.
   Values
   ascSort ascending
   descSort descending
reverse (deprecated)booleanoptional[b] false Whether to sort in descending order. This parameter is deprecated: use order instead. (deprecated in 1.100)
   Values
   falseSort ascending
   trueSort descending
typearray of stringsoptional[b] number Set the data type of the fields to influence the sorting order.
   Values
   anyAny fields.
   hexTreat the field as a hexadecimal value and sort in numerical order
   numberTreat the field as numerical and sort in numerical order
   stringTreat the field as string values and sort alphabetical

[a] The parameter name field can be omitted.

[b] Optional parameters use their default value unless explicitly set.

Hide omitted argument names for this function

Show omitted argument names for this function

If the order or type parameter is a single value, all fields are sorted with the same order or type.

Setting the type field tells sort() how to compare the individual values, either using lexicographical order (for example, string), numerical magnitude (for example, number, hex). The hex argument supports numbers as strings starting with either 0x, 0X, or no prefix.

Warning

Sorting is performed in memory. For optimum performance avoid sorting large numbers of events. Perform filtering first and sort the results, or use aggregation to simplify the event list before sorting. Putting sort() last in a query, after an aggregating function will offer the best performance.

sort() Syntax Examples

As an example, suppose you have LogScale ingesting data from a web server and want to get a list of referring domains and sub-domains. That is to say, the URLs without the path and web page from which it came (for example, just www.example.com from http://www.example.com/sales/great-sites.html). Suppose further that you want a count of each referrer and want to sort them alphabetically in reverse order — because we're fickle like that. Below is an example of how that might be done:

logscale
regex(regex="/.*/(?<ref_url>.+?)(/
| $)", field=referrer)
| groupBy(ref_url)
| sort(ref_url, type=string, order=desc, limit=12)

In the first line here, we're using the regex() function to extract just the first part of the URL, dropping everything after the domain and first slash. On the second line we're using groupBy() to group on each referrer (for example, ref_url) and counting them. If we hadn't stripped out the base URL from the first line, we might have multiple entries for each domain. See the regex() and groupBy() reference pages for more information on those query functions.

In the third line of the query above, notice the parameters for the sort() function. First is the new field ref_url we created in the first line, then type parameter to specify the values sorted may include characters or numbers. The second is how to order the results: in this case, in descending order. Last is the limit parameter to limit the results to the first twelve in the sorted list of results. The table below shows the results.

ref_url_count
www.zabotkin.ru14
www.tangblog.top1
www.skidn.com1
www.klfd.net1
www.iopt.pro42
www.google.com.au1
www.google.com11
www.bing.com3

To sort events by ascending or descending value, use the order parameter, for example:

logscale
groupBy(ref_url)
| sort(ref_url, type=string, order=asc, limit=12)

The output has been ordered using a string based (for example, alphabetical) sorting method.

The limit parameter to sort() limits the number of rows returned by the function after the sort has been completed. For example, to sort metrics by the repository name and then output the top entry:

name=datasource-count
|groupBy([repo])
|sort(field=_count,type=number,order=desc,limit=1)

The above query works by sorting the grouped counts (in the _count field), explicitly setting the type to number and then sorting by high to lowest (descending order).

To sort events by ascending or descending value, use the order parameter, for example:

logscale
groupBy(ref_url)
| sort(ref_url, type=string, order=desc, limit=12)

When using multiple fields, supply a corresponding array to order parameter as the field parameter, for example:

logscale
groupBy([ref_url,method])
| sort([ref_url,method], type=string, order=[desc,asc], limit=12)

sort()Examples

Click + next to an example below to get the full details.

Analyze User Sessions Based on Click Activity

Analyzes user sessions based on users click activity using the session() function

Query
logscale
groupBy(cookie_id, function=session(maxpause=15m, count(as=clicks)))
| sort(clicks)
Introduction

In this example, the session() function is used to analyze user sessions based on users click activity. The session() function groups events by a given timespan.

Example incoming data might look like this:

timestampcookie_idaction_typepage_urluser_agent
2025-05-15 05:30:00user123pageview/homeMozilla/5.0 (Windows NT 10.0; Win64; x64)
2025-05-15 05:30:15user123click/productsMozilla/5.0 (Windows NT 10.0; Win64; x64)
2025-05-15 05:30:30user123click/product/item1Mozilla/5.0 (Windows NT 10.0; Win64; x64)
2025-05-15 05:31:00user123click/cartMozilla/5.0 (Windows NT 10.0; Win64; x64)
2025-05-15 05:31:30user123click/checkoutMozilla/5.0 (Windows NT 10.0; Win64; x64)
2025-05-15 05:35:00user456pageview/homeMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
2025-05-15 05:35:30user456click/aboutMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
2025-05-15 05:36:00user456click/contactMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
2025-05-15 05:38:00user789pageview/homeMozilla/5.0 (iPhone; CPU iPhone OS 14_0)
2025-05-15 05:38:30user789click/productsMozilla/5.0 (iPhone; CPU iPhone OS 14_0)
Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    groupBy(cookie_id, function=session(maxpause=15m, count(as=clicks)))

    Groups events by the field cookie_id (unique user identifier) and creates sessions with 15-minute inactivity timeout (the default value of the maxpause parameter), then makes a count of each event in a session returning the result in a new field named clicks.

  3. logscale
    | sort(clicks)

    Sorts the results by number of clicks (default is descending order).

  4. Event Result set.

Summary and Results

The query is used to analyze user sessions based on the users click activity. The query is useful, for example, to identify most/least active user sessions, detect potential automated behavior or just to understand user engagement levels.

Sample output from the incoming example data:

cookie_idclicks
user1235
user4563
user7892

Note that each row represents an event (either pageview or click).

Calculate Query Cost for All Users by Repository

Search across multiple repositories to calculate query costs for all users by repository using sort() and groupBy() functions

Query
logscale
#type=humio #kind=logs class=c.h.j.RunningQueriesLoggerJob message="Highest Cost query"
 | groupBy(repo, initiatingUser, totalLiveCost, totalStaticCost)
 | sort([totalLiveCost, totalStaticCost])
Introduction

In this example, the query uses sort() and groupBy() functions to find query costs. The query filters logs in humio repository that are tagged with kind equal to logs and then returns the events where the class field has values containing c.h.j.RunningQueriesLoggerJob, searching for the specific value Highest Cost query.

Example incoming data might look like this:

#type#kindclassmessagetimestampdataspaceinitiatingUsertotalLiveCosttotalStaticCostdeltaTotalCostrepo
humiologsc.h.j.RunningQueriesLoggerJobHighest Cost query2025-03-26T09:30:00Zproductionjohn.doe15008002300security-logs
humiologsc.h.j.RunningQueriesLoggerJobHighest Cost query2025-03-26T09:31:00Zdevelopmentjane.smith200012003200app-logs
humiologsc.h.j.RunningQueriesLoggerJobHighest Cost query2025-03-26T09:32:00Zstagingbob.wilson10005001500infra-logs
humiologsc.h.j.RunningQueriesLoggerJobHighest Cost query2025-03-26T09:33:00Zproductionjohn.doe18009002700security-logs
humiologsc.h.j.RunningQueriesLoggerJobHighest Cost query2025-03-26T09:34:00Zdevelopmentjane.smith250013003800app-logs
humiologsc.h.j.RunningQueriesLoggerJobHighest Cost query2025-03-26T09:35:00Zstagingalice.cooper12006001800infra-logs
Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    #type=humio #kind=logs class=c.h.j.RunningQueriesLoggerJob message="Highest Cost query"

    Filters for Humio internal logs containing c.h.j. RunningQueriesLoggerJob in the class field and where the value in the message field is equal to Highest Cost query.

  3. logscale
    | groupBy(repo, initiatingUser, totalLiveCost, totalStaticCost)

    Groups the results by the repo field, the initiatingUser field, and by both cost types (the fields totalLiveCost, totalStaticCost), and returns a count in a field named _count.

  4. logscale
    | sort([totalLiveCost, totalStaticCost])

    Sorts the results by both the totalLiveCost field and the totalStaticCost field, in descending order by default.

  5. Event Result set.

Summary and Results

The query is used to search across multiple repositories and output query costs for all users by repository. The query returns the count in a field named _count. Use this query to focus on live and static costs separately.

Sample output from the incoming example data:

repoinitiatingUsertotalLiveCosttotalStaticCost_count
app-logsjane.smith200012001
security-logsjohn.doe15008001
infra-logsbob.wilson10005001

Filter Out Fields With No Value

Filter out fields with no values from search results

Query
logscale
method=GET
groupBy(field=[method, statuscode], function=count(as=method_total))
sort([method, statuscode], order=asc)
FieldName!=""
Introduction

It is possible to filter out on fields with no values in a given returned search result. In this example, all statuscode fields containing no value is filtered out from the final search result.

Example incoming data might look like this:

methodstatuscodemethod_total
GET<no value>10
GET20032492
GET3011
GET304113
GET4039
GET404132

Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    method=GET

    Filters for all events with methods of the type GET.

  3. logscale
    groupBy(field=[method, statuscode], function=count(as=method_total))

    Groups the returned results into a method field and a statuscode field and makes a count of the events in a new field named method_total.

  4. logscale
    sort([method, statuscode], order=asc)

    Sorts the returned results in ascending order.

  5. logscale
    FieldName!=""

    Excludes all events where one of the fields do not contain a value.

  6. Event Result set.

Summary and Results

The query is used to filter out fields not containing any values from the returned search result.

Sample output from the incoming example data:

methodstatuscodemethod_total
GET20032492
GET3011
GET304113
GET4039
GET404132

Find Least Common Values of a Field

Find the least common values of a field using the sort() function with count()

Query
logscale
groupby([type,kind], limit=max, function=count())
| sort(_count, order=asc)
Introduction

The sort() function can be used to sort values in a field bottom up. This is the opposite of the top() function, that can be used to find the most common values of a field in a set of events.

In this example, the sort() function is used to find the least common values of a field. Types of parsers used (type field) are grouped with their LogScale logging types (kind field) to find the least common values when searching for events of, for example, kinds metrics.

Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    groupby([type,kind], limit=max, function=count())

    Groups the event set by the fields type and kind. For each unique combination of type and kind, it then counts the number of events, and returns the results in a field named _count.

    The parameter limit is set to max to return all counts, as the purpose of this example is to find the least common values of a field.

  3. logscale
    | sort(_count, order=asc)

    Sorts the returned results in ascending order from the least common combinations to the most common combinations of type and kind.

  4. Event Result set.

Summary and Results

The query is used to find the least common values of a field in a set of events, in this example, identifying the least and most common type/kind combinations in an event set. Identifying rare combinations of type and kind could indicate unusual events or potential security issues. This type of query is useful in various scenarios, particularly for data analysis and understanding patterns in event sets.

Sort Timestamps With groupBy()

Sorting fields based on aggregated field values

Query

Search Repository: humio

logscale
timestamp := formatTime(format="%H:%M")
| groupBy([thread],
function=[{sort("timestamp")
| collect("timestamp")}])
Introduction

When using aggregation, you may want to sort on a field that is part of the aggregated set but not the main feature of the aggregated value. For example, sorting the values by their timestamp rather than the embedded value. To achieve this, you should use a function that sorts the field to be used as the sort field, and then use collect() so that the value from before the aggregation can be displayed in the generated event set. This query can be executed in the humio respository.

Step-by-Step
  1. Starting with the source repository events.

  2. logscale
    timestamp := formatTime(format="%H:%M")

    Creates a new field, timestamp formatted as HH:MM.

  3. logscale
    | groupBy([thread],

    Groups the events, first by the name of the thread and then the formatted timestamp.

  4. logscale
    function=[{sort("timestamp")
    | collect("timestamp")}])

    Uses the sort() combined with collect() as the method fo aggregation. As an embedded expression for the function, this will sort the events on the timestamp field and then retrieve the field as it would normally be removed as part of the aggregation process.

  5. Event Result set.

Summary and Results

The result set will contain a list of the aggregated thread names sorted by the timestamp:

threadtimestamp
BootstrapInfoJob10:09
DataSynchJob10:09
Global event loop10:10
LocalLivequeryMonitor10:09
LogCollectorManifestUpdate10:09
TransientChatter event loop10:10
aggregate-alert-job10:09
alert-job10:09
block-processing-monitor-job10:09
bloom-scheduler10:09
bucket-entity-config10:09
bucket-overcommit-metrics-job10:09
bucket-storage-download10:09
bucket-storage-prefetch10:09
chatter-runningqueries-logger10:09
chatter-runningqueries-stats10:09