sortSORT Command Guide - Sort Lines in Files with Windows CMD
Learn how to use the SORT command to alphabetically or numerically sort lines in text files. Includes syntax, /r and /n options, examples, and batch scripting use cases.
The SORT command is a Windows Command Prompt utility that reads input from a file (or stdin) and outputs the lines sorted alphabetically or numerically. Use SORT file.txt for ascending order, SORT /r file.txt for reverse order, or SORT /n file.txt for numeric sorting. Essential for data processing, log analysis, and batch scripting.
Whether you're organizing log files for analysis, preparing data for import, deduplicating lists, or building automated report pipelines, the SORT command provides a simple, fast way to reorder text line-by-line. System administrators and developers use SORT in combination with other commands (FINDSTR, MORE, redirection) for powerful text processing workflows.
This comprehensive guide covers SORT syntax, all options (/r, /n), practical examples for common scenarios, piping and redirection, troubleshooting tips, related commands, and frequently asked questions. By the end, you'll confidently sort files from the command line for data preparation and scripting.
What Is the SORT Command?
The SORT command is a built-in Windows utility that:
- Reads line-by-line – Processes text files one line at a time
- Sorts alphabetically by default – Case-insensitive, ascending order
- Supports reverse order –
/rfor descending - Supports numeric sort –
/nwhen lines begin with numbers - Accepts file input or stdin – Use redirection or piping for flexibility
SORT works in Command Prompt (CMD), PowerShell (as CMD command), and batch scripts. Available in all Windows versions. It does not modify the original file; output goes to stdout unless redirected.
SORT Command Syntax
SORT [/r] [/n] [[drive:][path]filename]
Parameters
| Parameter | Description |
|---|---|
/r | Reverse sort order (descending, Z to A) |
/n | Numeric sort (sorts by leading numbers) |
filename | File to sort. If omitted, reads from stdin |
Examples
Basic Alphabetical Sort
SORT names.txt
Input (names.txt):
Charlie
Alice
Bob
Output:
Alice
Bob
Charlie
Reverse Order Sort
SORT /r names.txt
Output:
Charlie
Bob
Alice
Numeric Sort
SORT /n scores.txt
Input (scores.txt):
100
9
42
15
Output:
9
15
42
100
Sort and Save to New File
SORT data.txt > sorted_data.txt
Sort and Remove Duplicates (with piping)
SORT file.txt | MORE +0
For deduplication, combine with a script or use PowerShell's Sort-Object -Unique.
Common Use Cases
- Log file organization – Sort log entries by timestamp or category for analysis
- Data preparation – Sort CSV or text data before import
- List deduplication – Sort first, then process to remove duplicates
- Report generation – Order output for readable reports
- Batch processing – Sort files as part of multi-step pipelines
Tips and Best Practices
- SORT is case-insensitive by default
- Use
/nonly when lines start with numbers; otherwise results may be unexpected - Redirect output to a new file to preserve the original
- For large files, SORT loads content into memory; very large files may be slow
Troubleshooting
"The system cannot find the file specified"
Verify the file path. Use full path or ensure you're in the correct directory.
"Invalid parameter"
Check that /r and /n are spelled correctly and use forward slashes.
Unexpected numeric sort results
Ensure numbers are at the beginning of lines. SORT /n interprets leading numeric portions.
Related Commands
findstr – Search and Filter
Use FINDSTR to filter lines before piping to SORT for combined search-and-sort workflows.
more – Paginate Output
Pipe SORT output to MORE for paginated viewing: SORT file.txt | MORE
type – Display File
TYPE displays file contents; SORT processes and reorders them.
Frequently Asked Questions
Does SORT modify the original file?
No. SORT writes to stdout. Use redirection (SORT file.txt > sorted.txt) to save sorted output to a new file.
How do I sort in reverse order?
Use the /r switch: SORT /r file.txt
What is the difference between SORT and SORT /n?
SORT sorts alphabetically (e.g., "10" comes before "2"). SORT /n sorts numerically (2 before 10).
Can SORT handle large files?
SORT loads the entire file into memory. Very large files (hundreds of MB) may cause performance issues. Consider PowerShell's Sort-Object for streaming or splitting large files.
Can I sort multiple files at once?
SORT accepts one filename. To sort multiple files, concatenate first: TYPE file1.txt file2.txt | SORT > combined_sorted.txt
Quick Reference Card
| Command | Purpose | Example |
|---|---|---|
SORT file.txt | Sort ascending | Basic sort |
SORT /r file.txt | Sort descending | Reverse order |
SORT /n file.txt | Numeric sort | Numbers first |
SORT file.txt > out.txt | Sort and save | Output to file |
Summary
The SORT command provides fast, simple line-by-line sorting for text files. Use it for alphabetical or numeric ordering, with options for reverse order. Combine with redirection and other commands for powerful text processing. Practice in the simulator or browse the commands reference for more Windows command guides.