Home > Commands N-Z > Commands S


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Reads input, sorts data, and writes the results to the screen, a file, or another device.

SORT acts as a filter, reading characters in a specified column and rearranging them in ascending or descending order.



SORT [/L[OCALE] locale] [/M[EMORY] kilobytes] [/O[UTPUT] [drive:][pathname]] [/R] [/REC[ORD_MAXIMUM] characters] [/T[EMPORARY] [drive:][path]] [/+n] [<] [[drive1:][path1]filename1] [> [drive2:][path2]filename2]

[command |] SORT [/L[OCALE] locale] [/M[EMORY] kilobytes] [/O[UTPUT] [drive:][pathname]] [/R] [/REC[ORD_MAXIMUM] characters] [/T[EMPORARY] [drive:][path]] [/+n] [> [drive2:][path2]filename2]

drive1: ( path1 filename1 v2.0 Win95 NT3.5)
Specifies the location and name of the file whose data you want to sort. If not specified, the standard input is sorted. Specifying an input file is faster than redirecting the same file as standard input.
drive2: ( path2 filename2 v2.0 Win95 NT3.5)
Specifies the location and name of a file in which the sorted output is to be stored.
command (v2.0 Win95 NT3.5)
Specifies a command whose output is the data you want to sort.

/? (NTXP)
Displays help.
/L[OCALE] locale (NT2000)
Override the system default locale with. The "C" locale yields a faster collating sequence. The sort is always case insensitive.
/M[EMORY] kilobytes (NT2000)
The amount of RAM to use for the sort. The best performance is usually achieved by not specifying a memory size. SORT will only create a temporary file when required by limitations in available memory.
/+n (v2.0 Win95 NT3.5)
Sorts the file according to the character in column n. If you do not use this switch, the SORT command sorts data according to the characters in column 1.
/O[UTPUT] [drive:][pathname] (NT2000)
The file where the sorted input is to be stored. If not specified, the data is written to standard output. Specifying an output file is faster than redirecting standard output to a file.
/R (v2.0 Win95 NT3.5)
/R[EVERSE] (NT2000)
Reverses the order of the sorting operation; that is, sorts from Z to A, and then from 9 to 0.
/REC[ORD_MAXIMUM] characters (NT2000)
The maximum number of characters in a row or record (default 4096, maximum 65535).
/T[EMPORARY] [drive:][path] (NT2000)
The path of the directory to hold SORT's working storage, in case the data does not fit in RAM. The default is %TEMP%.


For information about displaying information one screen at a time, see the MORE command.
TYPE - Display the contents of a text file.
Redirection - Redirect files, command output and error messages.
Equivalent Linux BASH commands:
sort - Sort text files.

Specifying a source

Unless you specify the command or filename parameter, SORT acts as a filter and takes input from the operating system standard input (usually from the keyboard, from a pipe, or from a file).

Using redirection symbols with SORT

You can use the pipe (|) or the less-than sign (<) to direct data through the SORT command from command or filename. If you want to display the information one screen at a time or direct the information to a file, you can also specify the MORE command or a filename. You can use the greater-than sign (>) to direct the sorted output to a file.

Before using a pipe for redirection, you should set the TEMP environment variable in your AUTOEXEC.BAT file.

Collating sequence

The SORT program uses the collating-sequence table corresponding to the country code and code-page settings. Characters greater than ASCII code 127 are sorted based on information in the COUNTRY.SYS file or in an alternate file specified by the COUNTRY command in your CONFIG.SYS file.

Uppercase vs. lowercase

SORT does not distinguish between uppercase and lowercase letters.

Limits on file size

The SORT command can handle files as large as 64K.

Limits on line length

The SORT command has problems with lines longer than 510 characters.


This command reads the file EXPENSES.TXT, sorts it in reverse order, and displays it on your screen:


Suppose you want to search a large file named MAILLST.TXT for the text "Jones", and suppose you want to sort the results of the search. To do this, use the pipe (|) to direct the output of a FIND command to the SORT command, as shown in the example:


The command produces a sorted list of lines that contain the specified text.

To sort keyboard input and display the results alphabetically on the screen, you can first use the SORT command with no parameters, as the example shows:


Then type the text you want sorted, pressing ENTER at the end of each line. When you have finished typing text, press CTRL+Z (^Z), and then press ENTER. The SORT command displays the text you typed, sorted alphabetically. You could also redirect sorted keyboard input to a file.



v2.0 v2.01 v2.05 v2.10 v2.11 v2.11R v2.12 v2.2 v2.25 v3.0 v3.20 v3.05 v3.1 v3.21 v3.25 v3.30 v3.3A v3.3R v3.3T v3.31 v3.40 v4.0 v4.01 v4.01A v5.0 v5.0A v5.00.02 v5.001A v5.01 v5.02 v6.0 v6.10 v6.2 v6.21 v6.22 v6.23 v7.00 v7.0R1 v7.10 v8.00
Win95 Win98 WinME
Windows NT
NT3.5 NT4 NT2000 NTXP NT2003