The command: "SORT" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available
The examples for the command "SORT"
The
`SORT` command in Windows Command Prompt is used to sort the contents of files. Here are some examples of using the
`SORT` command:
Example 1: Sorting text in a file:
@ECHO OFF
REM Create a text file with unsorted data
ECHO banana >> data.txt
ECHO apple >> data.txt
ECHO cherry >> data.txt
REM Sort the data in the file
SORT data.txt
REM Display the sorted data
TYPE data.txt
Description: This example creates a text file (
`data.txt`) with unsorted data. The
`SORT data.txt` command sorts the data in the file alphabetically. The
`TYPE` statement displays the sorted data.
Example 2: Reverse sorting:
@ECHO OFF
REM Create a text file with unsorted data
ECHO banana >> data.txt
ECHO apple >> data.txt
ECHO cherry >> data.txt
REM Sort the data in reverse order
SORT /R data.txt
REM Display the reverse sorted data
TYPE data.txt
Description: Here the
`/R` option is used to sort the data in reverse order. The
`TYPE` statement displays the reverse sorted data.
Example 3: Sorting numeric data:
@ECHO OFF
REM Create a text file with numeric data
ECHO 10 >> data.txt
ECHO 5 >> data.txt
ECHO 20 >> data.txt
REM Sort the numerical data
SORT /N data.txt
REM Display the sorted numerical data
TYPE data.txt
Description: Here the
`/N` option is used to sort numeric data. The
`TYPE` statement displays the sorted numeric data.
Example 4: Sorting files in current directory:
@ECHO OFF
REM Sort files in the current directory by filename
DIR /B /O:N > filelist.txt
SORT filelist.txt
REM Display the sorted list of files
TYPE filelist.txt
Description: This example uses the
`DIR` statement to create a list of files in the current directory.
`/O:N` sorts by filename, and the
`SORT` statement sorts this list. The
`TYPE` statement displays the sorted list of files.
Example 5: Sorting data from a pipeline:
@ECHO OFF
REM Use a pipeline to sort data
ECHO banana > data.txt
ECHO apple >> data.txt
ECHO cherry >> data.txt
TYPE data.txt | SORT
Description: Here the
`SORT` statement is used in a pipeline to sort the data directly from another statement (in this case
`TYPE`).
The
`SORT` statement provides various options that can be customized depending on your sorting needs. You can type
`SORT /?` in the command prompt to see a list of all available options and learn more details.
"SORT" Excerpt from Microsoft Windows Help
Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.
C:\\WINDOWS>
SORT [/R] [/+n] [/M kilobytes] [/L locale] [/REC recordbytes]
[[drive1:][path1]filename1] [/T [drive2:][path2]]
[/O [drive3:][path3]filename3]
/+n Specifies the character number, n, to
begin each comparison. /+3 indicates that
each comparison should begin at the 3rd
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.
/L[OCALE] locale Overrides the system default locale with
the specified one. The ""C"" locale yields
the fastest collating sequence and is
currently the only alternative. The sort
is always case insensitive.
/M[EMORY] kilobytes Specifies amount of main memory to use for
the sort, in kilobytes. The memory size is
always constrained to be a minimum of 160
kilobytes. If the memory size is specified
the exact amount will be used for the sort,
regardless of how much main memory is
available.
The best performance is usually achieved by
not specifying a memory size. By default the
sort will be done with one pass (no temporary
file) if it fits in the default maximum
memory size, otherwise the sort will be done
in two passes (with the partially sorted data
being stored in a temporary file) such that
the amounts of memory used for both the sort
and merge passes are equal. The default
maximum memory size is 90% of available main
memory if both the input and output are
files, and 45% of main memory otherwise.
/REC[ORD_MAXIMUM] characters Specifies the maximum number of characters
in a record (default 4096, maximum 65535).
/R[EVERSE] Reverses the sort order; that is,
sorts Z to A, then 9 to 0.
[drive1:][path1]filename1 Specifies the file to be sorted. If not
specified, the standard input is sorted.
Specifying the input file is faster than
redirecting the same file as standard input.
/T[EMPORARY]
[drive2:][path2] Specifies the path of the directory to hold
the sort's working storage, in case the data
does not fit in main memory. The default is
to use the system temporary directory.
/O[UTPUT]
[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.
Important information, tips for the "SORT" command
There are a few important points to note when using the
`SORT` command in the Windows Command Prompt:
1.
Sorting Type: By default, the
`SORT` command uses alphanumeric sorting, which means the data is sorted by both letters and numbers. You can enable numerical sorting using the
`/N` option.
SORT /N files.txt
2.
Standard Output: If you use
`SORT` without specified output file, the sorted output will be displayed on the screen. If you want to save the sorted data to a file, you can redirect the output to a file:
SORT unsortedData.txt > sortedData.txt
3.
Case sensitive: The sorting is not case sensitive by default. This means that upper and lower case letters are ignored when sorting. If case-sensitive sorting is required, you can use the
`/C` option.
SORT /C files.txt
4.
Reverse Sorting: Using the
`/R` option, you can reverse the sorting and display the data in descending order.
SORT /R files.txt
5.
Character Encoding: By default, sorting is performed using the native character encoding. If your data has a different character encoding, you can use the
`/UTF-8` option (from Windows 10, version 1903).
SORT /UTF-8 files.txt
6.
Character Limits: In older versions of Windows, there is a limit on the number of characters that
`SORT` can process at a time. If you encounter problems when sorting very long rows, this could be the cause.
7.
Custom Separators: The
`SORT` command normally separates entries using spaces. If your data uses a different delimiter, you could use tools like
`sed` or
`awk` in a shell environment before passing it to
`SORT`.
TYPE data.txt | sed 's/,/ /g' | SORT
It is important to consider the specific needs of your data and the available options of the
`SORT` command to achieve the desired result. You can use
`SORT /?` in the command prompt to see a list of available options and more details.