by Barry Dysert
(last updated October 12, 2020)
Hopefully, you're used to working at the Windows command line. The command line opens up a whole set of possibilities that are much more difficult or even impossible to do if you only stay within the Windows GUI. One feature that you find at the command line is the Sort utility. You can't directly sort the contents of a file in the GUI. People have found workarounds for sorting, like importing the file into Word or Excel, sorting the contents, then exporting the sorted results back out to a text file. This is both cumbersome and quite unnecessary if you use the command line.
The command line features the Sort utility. It's quite simple to use, it's fast, and it saves you from jumping through hoops to sort the contents of a file. Sort accepts a few switches, but most of the time you only need to be concerned with one or two. To see the complete help on Sort, including all of its switches, just type the following at a command prompt:
C:\> SORT /?
Of all the switches listed for Sort, the "/O" switch is doubtless the most used. It allows you to specify an output file for the sorted contents. So, for example, if you have a file called PartNumbers.txt, you can create a sorted version of that file by typing the following line:
C:\> SORT PartNumbers.txt /O PartNumbers.srt.txt
By default, Sort produces output sorted in ascending alphanumerical order, i.e., A-Z and 0-9. (The Sort command is always case insensitive.) If you want your file to be sorted in reverse order, specify the "/R" switch like so:
C:\> SORT PartNumbers.txt /R /O PartNumbers.rev.txt
A third commonly used switch is "+n", where "n" is a number. By specifying this switch, you tell Sort on which character to begin sorting. For instance, say that your PartNumbers.txt file lists part numbers that are formatted like this:
MO3468 ZM2596 IR5022 HU6531 FR8896 VE9321 RJ6417 PX8475 ZF6727 VF2679
A normal, ascending sort will produce this output:
FR8896 HU6531 IR5022 MO3468 PX8475 RJ6417 VE9321 VF2679 ZF6727 ZM2596
But let's say you're not interested in the first two characters of each line and instead want the output to be sorted based on the numeric portion of the part number. The numeric portion begins at position 3, so your sort command would look like this:
C:\> SORT PartNumbers.txt /+3 /O PartNumbers.srt3.txt
Now your output file will look like this:
ZM2596 VF2679 MO3468 IR5022 RJ6417 HU6531 ZF6727 PX8475 FR8896 VE9321
This tip (11680) applies to Windows 7, 8, and 10.
A niche command that sometimes comes in handy is the FOR loop. It has several forms and therefore can serve several ...Discover More
Command Prompt has been around for a while in various versions of Windows. With Windows 10 the Command Prompt window has ...Discover More
If your computer is set up in crowded environment, it may be helpful to establish times that an account can be used. You ...Discover More
FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."