Sort

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

Syntax

sort [/r] [/+n] [/m kilobytes] [/l locale] [/rec characters] [[drive1***:][path1]filename1] [/t** [drive2**:][path2]] [/o** [drive3**:***][path3]filename3]

[command |] sort [/r] [/+n] [/m kilobytes] [/l locale] [/rec characters] [[drive1***:][path1]filename1][/t** [drive2**:][path2]] [/o** [drive3**:***][path3]filename3]

Parameters

/r   : Reverses the sort order (that is, sorts from Z to A, and then from 9 to 0).

/+ n   : Specifies the character position number, n, at which sort begins each comparison.

/m   kilobytes   : Specifies the amount of main memory to use for the sort, in kilobytes (KB).

/l   locale   : Overrides the sort order of characters defined by the system default locale (that is, the language and Country/Region selected during installation).

/rec   characters   : Specifies the maximum number of characters in a record, or a line of the input file (the default is 4,096, and the maximum is 65,535).

[ drive1 : ][ path1 ] filename1   : Specifies the file to be sorted. If no file name is specified, the standard input is sorted. Specifying the input file is faster than redirecting the same file as standard input.

/t [ drive2 : ][ path2 ] : Specifies the path of the directory to hold the sort command's working storage, in case the data does not fit in main memory. The default is to use the system temporary directory.

/o [ drive3 : ][ path3 ] filename3   : Specifies the file where the sorted input is to be stored. If not specified, the data is written to the standard output. Specifying the output file is faster than redirecting standard output to the same file.

/?   : Displays help at the command prompt.

Remarks

  • Using the **/+**n command-line option

    In using the **/+**n command-line option, for example, /+3 indicates that each comparison should begin at the third character in each line. Lines with fewer than n characters collate before other lines. By default, comparisons start at the first character in each line.

  • Using the /m kilobytes command-line option

    The memory used is always a minimum of 160 KB. If the memory size is specified, the exact specified amount (but at least 160 KB) is used for the sort, regardless of how much main memory is available.

    The default maximum memory size when no size is specified is 90 percent of available main memory if both the input and output are files, and 45 percent of main memory otherwise. The default setting usually gives the best performance.

  • Using the /l locale command-line option

    Currently, the only alternative to the default locale is the "C" locale, which is faster than natural language sorting and sorts characters according to their binary encodings.

  • Specifying a source

    Unless you specify the command or FileName parameter, sort acts as a filter and takes input from the standard input (that is, usually from the keyboard, from a pipe, or from a file).

  • Using redirection symbols with sort

    You can use the pipe (|) symbol to direct data through the sort command from another command, or to direct the sort output to another command (for example, to the more command to display information one screen at a time). Using the less than symbol (<) or greater than symbol (>) to specify the input file or output file may not be very efficient; instead, specify the input file directly (as defined in the command syntax) and specify the output file using the /o parameter. This can be much faster, particularly with large files.

  • Uppercase vs. lowercase

    The sort command does not distinguish between uppercase and lowercase letters.

  • Limits on file size

    The sort command has no limit on file size.

  • Collating sequence

    The sort program uses the collating-sequence table that corresponds to the Country/Region 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.nt file.

  • Memory usage

    If the sort fits in memory (that is, either the default maximum memory size or as specified by the /m parameter), the sort is performed in one pass. Otherwise, the sort is performed in two passes, such that the amounts of memory used for both the sort and merge passes are equal. When two passes are performed, the partially sorted data is stored in a temporary file on disk. If there is not enough memory to perform the sort in two passes, a run-time error is issued. If the /m command-line option is used to specify more memory than is truly available, performance degradation or a run-time error can occur.

Examples

Sorting a file

The following command reads the file Expenses.txt, sorts it in reverse order, and displays it on your screen:

sort /r expenses.txt

Sorting the output from a command

To search a large file named Maillist.txt for the text "Jones," and to sort the results of the search, use the pipe (|) to direct the output of a find command to the sort command, as follows:

find "Jones" maillist.txt | sort

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

Sorting keyboard input

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

sort

Then type the text you want sorted, pressing ENTER at the end of each line. When you have finished typing text, press CTRL+Z, and then press ENTER. The sort command displays the text you typed, sorted alphabetically.

You can also redirect sorted keyboard input to a file. For more information, click redirect in the Related Topics list.

Formatting legend

Format

Meaning

Italic

Information that the user must supply

Bold

Elements that the user must type exactly as shown

Ellipsis (...)

Parameter that can be repeated several times in a command line

Between brackets ([])

Optional items

Between braces ({}); choices separated by pipe (|). Example: {even|odd}

Set of choices from which the user must choose only one

Courier font

Code or program output

Country

Using command redirection operators

Find

Command-line reference A-Z