The SORT command is used to read ASCII text input and to sort the lines in alphanumeric order.


To sort a text file:
SORT [/R] [/+n] filename
SORT [/R] [/+n] < filename

To sort the output of a Dos command:
command | SORT [/R] [/+n]

To sort keyboard input:
SORT [/R] [/+n] [> outputfile]
keyboard input first line
keyboard input last line

/RReverses the sort order; that is, sorts Z to A, then 9 to 0.
/+nSorts the file according to characters in column n.


  1. The input to SORT should be ASCII text structured so that each line can be considered a record of data. The lines (records) may be broken into fields, each beginning a fixed number of characters from the start of the line (eg. the output from a DIR command). Input in any other form tends to produce nonsense after being sorted (eg the output from a "readme.txt" file).

  2. SORT can handle files up to 64K and is not case sensitive

  3. The output from SORT is, by default, sent to the "Standard Output Device" (usually the screen), but can be redirected to any other device or a file using the redirection symbol (>).

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

  5. The Dos 7 version of SORT does appear to have a bit of a problem when handling very long lines. When sorting such lines, under certain conditions, some lines break up. An example of this anomalous behaviour has been posted in a newsgroup in which one line of three split when sorted on the 14th character. There were no problems when the sort was based on the first, 16th or 48th characters. Apparently the maximum "safe" line length is 510 characters/columns.


  1. To search a file named MailingList.txt for the text "Jones", and then sort the results of the search, use the pipe ( | ) to direct the output of a FIND command to the SORT command:
    FIND /I "Jones" MailingList.txt | SORT

  2. If there are too many entries to fit on a screen, add the MORE command:
    FIND /I "Jones" MailingList.txt | SORT | MORE

File Details

File NameDefault LocationDos Ver.Win Ver.SizeDateSource
Sort.exec:\windows\command 7.0Win95 25 80211/07/
7.1Win95 (OSR2.x) 25 882124/08/
Win98 25 882111/05/
Win98 SE 25 882123/04/

Superscripts denote which same size files, if any, are identical (using FC).

If you should have any comments or suggestions,
please contact: Bob Watson
This page last revised:
August 29, 2000.