sort

Description

The sort command sorts all of the results by the specified fields. Results missing a given field are treated as having the smallest or largest possible value of that field if the order is descending or ascending, respectively.

If the first argument to the sort command is a number, then at most that many results are returned, in order. If no number is specified, the default limit of 10000 is used. If the number 0 is specified, all of the results are returned. See the count argument for more information.

Syntax

sort [<count>] <sort-by-clause>... [desc]

Required arguments

<sort-by-clause>

Syntax: ( - | + ) <sort-field>, ( - | + ) <sort-field> ...

Description: List of fields to sort by and the sort order. Use a minus sign (-) for descending order and a plus sign (+) for ascending order. When specifying more than one field, separate the field names with commas. See Sort field options.

Optional arguments

<count>

Syntax: <int>

Description: Specify the number of results to return from the sorted results. If no count is specified, the default limit of 10000 is used. If 0 is specified, all results are returned.

Description: Determine automatically how to sort the values of the field.

ip

Syntax: ip(<field>)

Description: Interpret the values of the field as IP addresses.

num

Syntax: num(<field>)

Description: Interpret the values of the field as numbers.

str

Syntax: str(<field>)

Description: Interpret the values of the field as strings and order the values alphabetically.

Usage

By default, sort tries to automatically determine what it is sorting. If the field takes on numeric values, the collating sequence is numeric. If the field takes on IP address values, the collating sequence is for IPs. Otherwise, the collating sequence is in lexicographical order. Some specific examples are:

Alphabetic strings are sorted lexicographically.

Punctuation strings are sorted lexicographically.

Numeric data is sorted as you would expect for numbers and the sort order is specified as ascending or descending.

Alphanumeric strings are sorted based on the data type of the first character. If the string starts with a number, the string is sorted numerically based on that number alone. Otherwise, strings are sorted lexicographically.

Strings that are a combination of alphanumeric and punctuation characters are sorted the same way as alphanumeric strings.

In the default automatic mode for a field, the sort order is determined between each pair of values that are compared at any one time. This means that for some pairs of values, the order might be lexicographical, while for other pairs the order might be numerical. For example, if sorting in descending order: 10.1 > 9.1, but 10.1.a < 9.1.a.

Lexicographical order

Lexicographical order sorts items based on the values used to encode the items in computer memory. In Splunk software, this is almost always UTF-8 encoding, which is a superset of ASCII.

Numbers are sorted before letters. Numbers are sorted based on the first digit. For example, the numbers 10, 9, 70, 100 are sorted lexicographically as 10, 100, 70, 9.

Uppercase letters are sorted before lowercase letters.

Symbols are not standard. Some symbols are sorted before numeric values. Other symbols are sorted before or after letters.

Examples

Example 1:

Sort results by "ip" value in ascending order and then by "url" value in descending order.

... | sort num(ip), -str(url)

Example 2:

Sort first 100 results in descending order of the "size" field and then by the "source" value in ascending order. This example specifies the type of data in each of the fields. The "size" field contains numbers and the "source" field contains strings.

... | sort 100 -num(size), +str(source)

Example 3:

Sort results by the "_time" field in ascending order and then by the "host" value in descending order.

... | sort _time, -host

Example 4:

Change the format of the event's time and sort the results in descending order by the Time field that is created with the eval command.

Example 5.

Sort a table of results in a specific order, such as days of the week or months of the year, that is not lexicographical or numeric. For example, you have a search that produces the following table:

Day

Total

Friday

120

Monday

93

Tuesday

124

Thursday

356

Weekend

1022

Wednesday

248

Sorting on the day field (Day) returns a table sorted alphabetically, which does not make much sense. Instead, you want to sort the table by the day of the week, Monday to Friday. To do this, you first need to create a field (sort_field) that defines the order. Then you can sort on this field.

Comments

Leonjxtan
Thank you for your question. The sort command sorts all of the events in the results from the previous command, and then returns the number specified with the count argument. If the count argument is not specified, the sort command returns the first 10000 of the entire sorted list. If you want all of the events returned, you must specify zero ( 0 ) for the count (for example ... | sort 0 ...

Lstewart splunk, Splunker

May 19, 2017

<count>
Syntax: <int>
Description: Specify the number of results to sort. If no count is specified, the default limit of 10000 is used. If "0" is specified, all results are returned.
Default: 10000
-> Does it mean that by default, if i add sort command, it will only return first 10000 rows, not all rows? are these first 10000 rows first before sorting or first after sorting?

Leonjxtan

May 14, 2017

example 1 is a bit problematic - using the num option on an ip field when there is an ip option sounds odd - example shows ... | sort num(ip), -str(url

Paddygriffin

November 11, 2015

Do not trust auto sort, prefer to specify a format sort num(field) or sort str(field).

Ykherian, Splunker

February 1, 2013

Me anand1984, <br /><br />Nope. That really is the only way to do this particular type of sorting.

Enter your email address, and someone from the documentation team will respond to you:

Please provide your comments here. Ask a question or make a suggestion.

Feedback submitted, thanks!

You must be logged into splunk.com in order to post comments.
Log in now.

Please try to keep this discussion focused on the content covered in this documentation topic.
If you have a more general question about Splunk functionality or are experiencing a difficulty with Splunk,
consider posting a question to Splunkbase Answers.