Line Sorting and Text Processing for Developers
Line Sorting and Text Processing for Developers
Text processing — sorting, deduplicating, filtering, and transforming lines — is a daily task for developers. Whether you're cleaning data, organizing imports, or preparing config files, knowing the techniques saves time.
Sorting Algorithms in Practice
Alphabetical Sort
The default. But beware of locale differences: does "é" come after "e" or after "z"?
lines.sort(); // ASCII order: uppercase before lowercase
lines.sort((a, b) => a.localeCompare(b)); // Locale-aware
Natural Sort
Treats embedded numbers as numbers: "file2" comes before "file10".
lines.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
Reverse Sort
lines.sort().reverse();
Case-Insensitive Sort
lines.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
Deduplication
Remove Exact Duplicates
const unique = [...new Set(lines)];
Remove Case-Insensitive Duplicates
const seen = new Set();
const unique = lines.filter(line => {
const lower = line.toLowerCase();
if (seen.has(lower)) return false;
seen.add(lower);
return true;
});
Filtering
Remove Empty Lines
lines.filter(line => line.trim().length > 0);
Remove Lines Matching Pattern
lines.filter(line => !line.match(/^#/)); // Remove comments
Keep Lines Matching Pattern
lines.filter(line => line.includes("ERROR"));
Transformations
Trim Whitespace
lines.map(line => line.trim());
Add Prefix/Suffix
lines.map(line => - ${line}); // Bullet list
Number Lines
lines.map((line, i) => ${i + 1}. ${line});
Command Line Equivalents
sort file.txt # Alphabetical sort
sort -n file.txt # Numeric sort
sort -u file.txt # Sort and deduplicate
sort -r file.txt # Reverse sort
uniq file.txt # Remove adjacent duplicates
grep -v "^#" file.txt # Remove comment lines
wc -l file.txt # Count lines
Try It
Use our Line Sorter tool to sort, shuffle, deduplicate, and process text lines with a visual pipeline interface.
Conclusion
Text processing is a fundamental skill. Know the sort variants (natural, case-insensitive, reverse), deduplication methods, and filtering techniques. For quick tasks, use our browser-based tools. For scripting, use the Unix command line equivalents.