Sort

Arranges a variable's contents in alphabetical, numerical, or random order (optionally removing duplicates).

Sort, VarName [, Options]

 

Parameters

VarName The name of the variable whose contents will be sorted.
Options See list below.

 

Options

A string of zero or more of the following letters (in any order, with optional spaces in between):

C: Case sensitive sort (ignored if the N option is also present).

Dx: Specifies x as the delimiter character, which determines where each item in VarName begins and ends. If this option is not present, x defaults to linefeed (`n), which correctly sorts VarName if its lines end in either LF (`n) or CR+LF (`r`n).

N: Numeric sort: Each item is assumed to be a number rather than a string (for example, if this option is not present, the string 233 is considered to be less than the string 40 due to alphabetical ordering). Both decimal and hexadecimal strings (e.g. 0xF1) are considered to be numeric. Strings that do not start with a number are considered to be zero for the purpose of the sort. Numbers are treated as 64-bit signed integers, but in version v1.0.25+ they are treated as 64-bit floating point values so that the decimal portion of each number (if any) is taken into account.

Pn: Sorts items based on character position n (do not use hexadecimal for n). If this option is not present, n defaults to 1, which is the position of the first character. The sort compares each string to the others starting at its nth character. If n is greater than the length of any string, that string is considered to be blank for the purpose of the sort. When used with option N (numeric sort), the string's character position is used, which is not necessarily the same as the number's digit position.

R: Sorts in reverse order (alphabetically or numerically depending on the other options).

Random: Sorts in random order. This option causes all other options except D, Z, and U to be ignored. Examples:
Sort, MyVar, Random
Sort, MyVar, Random Z D|

U [v1.0.25+]: Removes duplicate items from the list so that every item is unique. ErrorLevel is set to the number of items removed (0 if none). If the C option is in effect, the case of items must match for them to be considered identical. If the N option is in effect, an item such as 2 would be considered a duplicate of 2.0. If either the Pn or \ (backslash) option is in effect, the entire item must be a duplicate, not just the substring that is used for sorting. If the Random option is in effect, duplicates are removed only if they appear adjacent to each other as a result of the sort. For example, when "A|B|A" is sorted randomly, the result could contain either one or two A's.

Z: To understand this option, consider a variable that contains RED`nGREEN`nBLUE`n. If the Z option is not present, the last linefeed (`n) is considered to be part of the last item, and thus there are only 3 items. But by specifying Z, the last `n (if present) will be considered to delimit a blank item at the end of the list, and thus there are 4 items (the last being blank).

\: Sorts items based on the substring that follows the last backslash in each. If an item has no backslash, the entire item is used as the substring. This option is useful for sorting bare filenames (i.e. excluding their paths), such as the example below, in which the AAA.txt line is sorted above the BBB.txt line because their directories are ignored for the purpose of the sort:
C:\BBB\AAA.txt
C:\AAA\BBB.txt
Note: Options N and P are ignored when the backslash option is present.

 

Remarks

This command is typically used to sort a variable that contains a list of lines, with each line ending in a linefeed character (`n). One way to get a list of lines into a variable is to load an entire file via FileRead.

If VarName is clipboard and the clipboard contains files (such as those copied from an open Explorer window), those files will be replaced with a sorted list of their filenames. In other words, after the operation, the clipboard will no longer contain the files themselves.

If VarName is an environment variable, a script variable will be created to take its place. To put the sorted contents back into the environment variable and force the script to access it in the future, use this example:

Sort, MyEnvVar
EnvSet, MyEnvVar, %MyEnvVar%
MyEnvVar =   ; Make the script's variable blank so that MyEnvVar will be retrieved from the environment in the future.

ErrorLevel is changed by this command only when the U option is in effect.

The maximum capacity of a variable can be increased via #MaxMem.

If a large variable was sorted and later its contents are no longer needed, you can free its memory by making it blank, e.g. MyVar =

 

Related

FileRead, file-reading loop, parsing loop, StringSplit, clipboard, #MaxMem

 

Example

MyVar = 5,3,7,9,1,13,999,-4
Sort MyVar, N D,  ; Sort numerically, use comma as delimiter.
MsgBox %MyVar%   ; The result is -4,1,3,5,7,9,13,999

; This example makes Win+C a hotkey to copy files from an open
; Explorer window and put their sorted filenames onto the clipboard:
#c::
Clipboard = ; Must be blank for detection to work.
Send ^c
ClipWait 2
if ErrorLevel <> 0
	return
Sort Clipboard
MsgBox Ready to be pasted:`n%Clipboard%
return