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

Using Sticky Keys

Sticky Keys is an accessibility feature designed for people who have trouble holding down two or more keys ...

Discover More

Using N.R.S. Ghost Control

Ghost Control is a nice utility when you need to save your keystrokes and mouse movements while doing repetitive tasks. ...

Discover More

Using Powercfg to Export and Import a Power Scheme

You can export to a file your power schemes and then import them later on. The only caveat is that you must be running as ...

Discover More
More WindowsTips

Generate a Wireless Network Report

If you have Wi-Fi connection problems, Windows 10 can provide a report to help diagnose possible issues. Using Command ...

Discover More

Changing Font Size in a Command Prompt Window

If you work at the command level very much, you may want to change the fonts that are used. You can control what ...

Discover More

Displaying All the Files in a Folder using the Command Prompt

Displaying all the files a folder contains is an easy task in Windows. One way you can display the files is using command ...

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 8 - 5?

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


Newest Tips