SORT

Sort will accept a redirected or piped file input and TYPE the file, sorted line by line.

Syntax
      SORT [options] 

Options

   /+n   Sort the file ignoring the first one or more characters in each row.
         The default (/+1) will sort using all characters in each row.
          /+2 will start comparing at the second character, /+3 the third etc.
         Lines with fewer than n characters collate before other lines.

   /C[ASE_SENSITIVE]

         Case sensitive sort. (undocumented switch in Win7 and above)
         Dependent on the locale some characters will sort before A ('- !#) or after Z (~£¬Ξ)

  [drive:][pathname]

         The file to be sorted.
         If not specified, the standard input is sorted.
         Specifying an input file is faster than redirecting the same file as standard input.

   /L[OCALE] locale

         Override the system default locale with the specified one.
         The "C" locale yields the fastest collating sequence and is currently the only alternative.
         The default sort with the C locale is case insensitive, so will not consistently place 'A' before or after 'a'.
         When both the C locale and Case-Sensitive are specified, all capitals will sort before lower case, so Z before a.
         The C locale is not the same collation as LC_ALL=C in Unix.

   /M[EMORY] kilobytes

         The amount of main memory to use for the sort, in KiB.
         The best performance is usually achieved by not specifying a memory size.

         The memory size is always constrained to be a minimum of 160 KiB.
         If the memory size is specified the exact amount will be used for the sort,
         regardless of how much main memory is available.

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

         SORT will only create a temporary file when required by limitations in available memory.

   /REC[ORD_MAXIMUM] characters 

         The maximum number of characters in a row or record
         (default 4096, maximum 65535)

   /R[EVERSE]  Reverse the sort order (Z to A, 9 to 0)

   /T[EMPORARY] [drive:][path]

         The path of the directory to hold SORT's working storage, in case the data does not fit in RAM.
         The default is the system temporary directory %temp%

   /O[UTPUT] [drive:][pathname]

         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 the same file.

   /U[NI_OUTPUT]
         Output in unicode. (undocumented switch in Win7 and above)

   /UNIQ[UE]
         Output only unique lines (undocumented switch in Windows 10 and above)

The sort order (collation) is not the same as that used by Windows Explorer (StrCmpLogicalW). In your default country locale the sort order should follow the sorting rules for that language. Windows Explorer has its own rather complex set of sorting rules.

In the default locale, many Unicode chracters e.g. the greek character Xi 'Ξ', or Omega 'Ω' will sort after 'Z' in both Windows Explorer, and in
'SORT /L C filename.txt'.

First → Last (Default Locale sort order)
'- !"#$%()*,./:;?@[\]^_`~+<=>£01..89aAbBcCdDeE...zZ

When the locale is set to C, some ASCII characters will sort after Z:
First → Last (C locale sort order)
!"#$%'()*+,-./01..89:;<=>?@[\]^_`aABbcCDdeE...zZ~£¬

Because the /C and /U options are undocumented some Unicode characters may not sort correctly.

Examples

Sort the file C:\demo\musiclist.txt:

SORT "C:\demo\musiclist.txt"

Sort a file and output only the unique values to a file:

TYPE demofile.txt | SORT /unique /o C:\work\unique.txt

Pipe the output of a DIR command into SORT:

DIR /b "C:\demo\" | SORT

Pipe the output from DIR into SORT, reverse the list and save into a file:

DIR /b | SORT /r /o demo.txt

Pipe the output from DIR into SORT, and include any errors in the output (by redirecting the error stream):

DIR /b "folder-that-doesnt-exist" 2>&1 | SORT /r /o demo.txt

"Cultivate peace and order before confusion and disorder" ~ Tao Teh Ching

Related commands

TYPE - Display the contents of a text file.
Redirection - Redirect files, command output and error messages.
Equivalent PowerShell: Sort-Object - Sort objects by property value (sort).
Equivalent bash command (Linux): sort - Sort text files.


 
Copyright © 1999-2024 SS64.com
Some rights reserved