Using the Sort Command

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

Author Bio

Barry Dysert

Barry has been a computer professional for over 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...

MORE FROM BARRY

Freeing Up Disk Space by Deleting Old OS Files

I'm always looking for ways to free up disk space. Even with a 1 TB drive I want to be a "good disk citizen." If you're ...

Discover More

Allowing Remote Desktop

Remote Desktop allows you to access other systems and operate them from your own system. If you want to use this ...

Discover More

Changing How User Account Control Works

The User Account Control (UAC) model allows an administrator to control when (or if) they are to be notified when a ...

Discover More
More WindowsTips

Clearing the Address Resolution Protocol Cache

Corrupt or unwanted Address Resolution Protocol (ARP) cache entries can cause connectivity errors. Here is how to flush ...

Discover More

Displaying the Command Prompt Window

You may have the need to perform repetitive operations or group several commands together to run as a batch. In such ...

Discover More

Command Prompt's New Tricks

Command Prompt has been around for a while in various versions of Windows. With Windows 10 the Command Prompt window has ...

Discover More
Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


Newest Tips