Programming Field

Sort - DOS/Command Prompt Reference

Reads each line of input text data as a string, sorts lines in ascending (or descending) order, and outputs.

* In Cygwin environment, or the environment that Git for Windows is installed and set to Path, ‘sort(.exe)’ may refer to those Linux-based sort program. To check whether Sort.exe of Windows command is executed, type ‘sort /?’ and check whether Sort's help is displayed.

[PowerShell] By default ‘sort’ is registered as an alias for ‘Sort-Object’ and when using ‘sort’ in PowerShell ‘Sort-Object’ will work. Options for ‘Sort-Object’ are much different from ones for ‘sort.exe’, but it is suitable to use ‘sort’ (Sort-Object) than ‘sort.exe’, because both commands have almost same behavior and when using ‘sort.exe’ in PowerShell you have to be careful with charset.


sort[.exe] [/R] [/+<number>] [<file-name>] [/M[EMORY] <memsize>] [/L[OCALE] <locale>]
  [/REC[ORD_MAXIMUM] <record-size>] [/T[EMPORARY] <temp-path>] [/O[UTPUT] <out-file>]


/R Sorts in reverse (descending; from Z to A) order. Without /R, sorts in normal (ascending; from A to Z) order.
/+<number> Specifies the character index (one-based) where to compare from. Specifies a numeric value to <number>.

[Windows NT?/XP or later] Specifies the input file. If omitted, the standard input is used. You can use input redirection, but specifying the input file is a little faster.

[MS-DOS, Windows 95/98/Me] Specifying the input file is not supported. Input redirection must be used instead.

/M[EMORY] <memsize>

[Windows NT?/XP or later] Specifies the memory size, in KB, of the work memory for sorting. If not specified, default size is used. (‘/M[EMORY]’ can be either ‘/M’ or ‘/MEMORY’.)

When the memory size is specified, its size is always used regardless of the current available main memory size. The default size is 90% of main memory when both input and output file are specified, and 45% of main memory when either or both input and output file are not specified.

/L[OCALE] <locale> [Windows NT?/XP or later] Specifies the locale (language) for sorting. <locale> will be the locale string, but currently only ‘C’ can be specified. (‘/L[OCALE]’ can be either ‘/L’ or ‘/LOCALE’.)
/REC[ORD_MAXIMUM] <record-size> [Windows NT?/XP or later] Specifies the maximum length of characters per one record (one line). <record-size> can be up to 65535. The default size is 4096. (‘/REC[ORD_MAXIMUM]’ can be either ‘/REC’ or ‘/RECORD_MAXIMUM’.)
/T[EMPORARY] <temp-path> [Windows NT?/XP or later] Specifies the directory where to create temporary files on insufficient memory for sorting. By default the system temporary directory is used. (‘/T[EMPORARY]’ can be either ‘/T’ or ‘/TEMPORARY’.)
/O[UTPUT] <out-file>

[Windows NT?/XP or later] Specifies the file name to write the sort result to. <out-file> will be the destination file name. If omitted, the results are printed to the standard output (STDOUT). The output redirection can be used, but using this option will be slightly faster. (‘/O[UTPUT]’ can be either ‘/O’ or ‘/OUTPUT’.)

[MS-DOS, Windows 95/98/Me] This option is not available. Use the output redirection instead.


Usage of Sort

This program is used for sorting each lines of a text file, as well as for sorting the output or etc. of another programs (such as Dir or Find) by using pipe ‘|’.

* Please note that the options are much different from Linux ‘sort’.


Sample 1

sort /+3 < foo.txt > bar.txt

Reads the content of ‘foo.txt’, sorts by line by comparing the third and subsequent characters of each line, and writes the result (ascending order) to ‘bar.txt’.

Sample 2

sort /+3 foo.txt /O bar.txt

[Windows NT?/XP or later] This sample is written without the redirections ‘<’ and ‘>’ and has the same result as Sample 1.

Sample 3

dir *.exe /B | sort /R | more

Enumerates files with extension ‘exe’ in the current directory by using Dir command, passes the result to Sort by using pipe ‘|’, and prints the data in descending order. When printing, uses More to display one screen at a time.