Using the Sort Command

by Barry Dysert
(last updated December 5, 2016)

3

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 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Using Two Monitors with Windows

Windows is great about letting you work on multiple things at the same time. Often, however, you end up with so many windows ...

Discover More

Ending a Process Using the Task Manager

One of the many functions of the Windows Task Manager is to allow you to control processes running on your system. If you ...

Discover More

Creating a System Image

A system image is a snapshot of your system disk as of a certain point in time. Should the need arise, you can restore your ...

Discover More
More WindowsTips

Displaying the Command Prompt in Windows 8

Some commands require the use of the command prompt window. How you display that essential window in Windows 8 can be a bit ...

Discover More

Introduction to Regular Expressions

Regular expressions are usually the domain of computer programmers. There are times, though, when an end-user might find that ...

Discover More

Copying Files Using the Command Line

The copy command can be a timesaver over trying to do the similar sort of thing with Windows Explorer. You can copy hundreds ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

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}] in your comment text. You’ll be prompted to upload your image when you submit the comment. 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 7 + 5?

2016-12-05 09:32:06

Mary

Excellent tip.


2016-12-05 09:24:22

Jennifer Thomas

Wow - that one brings takes me back to fond memories of the 'kinder, simpler' times of DOS .3; there was actually a lot of cool stuff in that and it still works so thanks for the reminder on this one.


2016-12-05 07:01:22

Lou Jones

What a great option! Why didn't my 952-page Windows 10 All-In-One for Dummies book even mention its existence?


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.