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

How to Run a Program when Windows Starts

It's easy to have a program run when Windows starts. In fact, by using the Task Scheduler, you can have all sorts of ...

Discover More

Changing User Permissions for an Entire Drive

All objects on your computer (e.g., disk drives) have permissions that allow or deny various types of access. This tip ...

Discover More

Understanding Indexed Search

Windows has a powerful search utility that allows you to search for files that may be scattered across separate folders ...

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

Setting Time Limits for Windows 10 Users

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

Counting Files and Subfolders Using the Command Prompt

There are times when you might need to know how many files or subfolders are in a folder. Using the command prompt, you ...

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 five minus 3?

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


Newest Tips