SORT - Sort Lines of Text
Have you ever wanted to sort a name list, a log, or the output of dir alphabetically? You can sort lines of text right in Command Prompt without opening a spreadsheet.
The SORT command sorts each line of text and displays the result. It works on file contents as well as on the output of other commands received through a pipe.
Basic Usage
The syntax of the SORT command is as follows.
SORT [options] [filename]
Suppose you have the following fruits.txt.
banana
Cherry
apple
When you specify a file name, the lines are displayed in ascending order.
SORT only prints the sorted result to the screen; it does not modify fruits.txt. How to save the result is described below.
By default, sorting is case-insensitive. In the example above, Cherry, which starts with an uppercase letter, is placed after banana.
In PowerShell, sort is an alias for Sort-Object, so the commands in this article do not behave the same way. When using SORT from PowerShell, type sort.exe.
Sorting Output with a Pipe
If you omit the file name, SORT sorts the text it receives from standard input. Combined with a pipe (|), you can sort the output of other commands.

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
The list of file names from dir /b is sorted by name.
You can also feed a file through type.
Input redirection works too.
SORT Command Options
The commonly used options are as follows.
| Option | Description |
|---|---|
/R | Sort in descending (reverse) order |
/+n | Compare starting at character position n |
/O file | Write the sorted result to the specified file |
/L locale | Specify the locale used for sorting (in practice only "C" is valid) |
/M kilobytes | Specify the amount of main memory to use for sorting, in KB |
/REC characters | Specify the maximum number of characters in a line (default 4,096, max 65,535) |
/T path | Specify the folder for temporary working files |
/R - Sort in Descending Order
Adding /R reverses the order.
/+n - Sort from a Column Position
By default, comparison starts at the beginning of each line. With /+n, it starts at character n. This is useful for fixed-width data that you want to sort by a field in the middle of the line.
Tanaka 85
Suzuki 92
Sato 78
The names are padded to 8 characters, so the score starts at character 9.
The lines are now in ascending order of score. Add /R for descending order.
When /+n is specified, lines with fewer than n characters are placed before all other lines. Blank lines are included.
/L C - Sort by Character Code
With /L C, sorting is independent of the locale and follows the raw character code values. Case is then significant, and uppercase letters come before lowercase ones (Cherry comes first).
Saving the Sorted Result to a File
To save the result, specify the destination with the /O option.
Of course, you can also save it with redirection (>).
If you redirect to the same file you are reading, as in sort fruits.txt > fruits.txt, fruits.txt is emptied before SORT can read it and its contents are lost. Always write the result to a different file.
Numbers Are Sorted as Characters
SORT compares numbers character by character, as text. As a result, they are not ordered by numeric value.
10
2
33
1
10 comes before 2 because its first character, 1, is smaller than 2. To sort by numeric value, pad the numbers with leading zeros so they all have the same number of digits.
10
02
33
01
If you want to sort by numeric value, PowerShell’s Sort-Object lets you specify a type and sort numbers as numbers.
Practical Examples
Sort Lines Extracted from a Log
You can sort lines extracted with findstr.
Show Running Processes by Name
Sort the output of tasklist by name. /nh omits the header row.
Review Long Results One Screen at a Time
If the sorted result is long, combine it with more.
SORT reads text according to the console’s code page. Feeding it a file saved as UTF-8, for example, may garble non-ASCII text or produce an unexpected order. See the related article on encoding for details.
Summary
| What you want to do | Command |
|---|---|
| Sort in ascending order | sort filename |
| Sort in descending order | sort /R filename |
| Sort command output | command | sort |
| Sort from a column position | sort /+n filename |
| Save the result to a file | sort filename /O output |
| Sort by character code | sort /L C filename |