SORT - Sort Lines of Text

Maintained on

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort fruits.txt
apple
banana
Cherry
C:\users\user>
The original file is not changed

SORT only prints the sorted result to the screen; it does not modify fruits.txt. How to save the result is described below.

Case is ignored

By default, sorting is case-insensitive. In the example above, Cherry, which starts with an uppercase letter, is placed after banana.

In PowerShell, type sort.exe

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b | sort
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

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

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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

The list of file names from dir /b is sorted by name.

You can also feed a file through type.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>type fruits.txt | sort

Input redirection works too.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort < fruits.txt

SORT Command Options

The commonly used options are as follows.

OptionDescription
/RSort in descending (reverse) order
/+nCompare starting at character position n
/O fileWrite the sorted result to the specified file
/L localeSpecify the locale used for sorting (in practice only "C" is valid)
/M kilobytesSpecify the amount of main memory to use for sorting, in KB
/REC charactersSpecify the maximum number of characters in a line (default 4,096, max 65,535)
/T pathSpecify the folder for temporary working files

/R - Sort in Descending Order

Adding /R reverses the order.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort /R fruits.txt
Cherry
banana
apple
C:\users\user>

/+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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort /+9 scores.txt
Sato 78
Tanaka 85
Suzuki 92
C:\users\user>

The lines are now in ascending order of score. Add /R for descending order.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort /+9 /R scores.txt
Lines shorter than n characters come first

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).

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort /L C fruits.txt
Cherry
apple
banana
C:\users\user>

Saving the Sorted Result to a File

To save the result, specify the destination with the /O option.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort fruits.txt /O sorted.txt

Of course, you can also save it with redirection (>).

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort fruits.txt > sorted.txt
Do not redirect to the original file

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
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort numbers.txt
1
10
2
33
C:\users\user>

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
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>sort numbers.txt
01
02
10
33
C:\users\user>
PowerShell is also an option for numeric sorting

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.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>findstr "ERROR" app.log | sort

Show Running Processes by Name

Sort the output of tasklist by name. /nh omits the header row.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>tasklist /nh | sort

Review Long Results One Screen at a Time

If the sorted result is long, combine it with more.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>dir /b | sort /R | more
Watch the character encoding

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 doCommand
Sort in ascending ordersort filename
Sort in descending ordersort /R filename
Sort command outputcommand | sort
Sort from a column positionsort /+n filename
Save the result to a filesort filename /O output
Sort by character codesort /L C filename

Quiz

Practice Problem

Which command displays the lines of data.txt in descending order?

回答がサーバーに送信されることはありません
Practice Problem

A file contains 10, 2, 33, and 1, one per line. What is the result of running sort on it without options?

回答がサーバーに送信されることはありません
Practice Problem

What happens when you run sort fruits.txt > fruits.txt?

回答がサーバーに送信されることはありません
#Command Prompt #Batch File #SORT #Command #Text Processing