Sort lines in 7 modes: alphabetical, reverse, numeric, random, by length, case-insensitive, and natural order. Remove duplicates and blank lines too.
Sort order
Paste lines and click Sort Lines
Sorting lines of text is a standard task in software development, data sanitisation, and log analysis. Whether ordering database schema exports, alphabetically sorting localisation dictionary keys, or re-ordering numeric identifiers, choosing the appropriate collation and sorting rules is essential.
| Sorting Mode | Description | Example Input | Example Output |
|---|---|---|---|
| Alphabetical (A-Z) | Standard ASCII/Unicode codepoint comparison | item10, item2, Item1 | Item1, item10, item2 |
| Case Insensitive (A-Z) | Lowercases strings before character comparison | banana, Apple, cherry | Apple, banana, cherry |
| Natural Sort | Considers multi-digit numbers as numbers rather than raw characters | file1, file10, file2 | file1, file2, file10 |
| Numeric Sort | Parses strings as integer or float values | 100, 25, 5 | 5, 25, 100 |
| Reverse Sort (Z-A) | Inverts the collation order | alpha, beta, gamma | gamma, beta, alpha |
In standard lexicographical sorting, the string "file10.txt" sorts before "file2.txt" because the character '1' has a lower ASCII code point than '2'.
Natural sorting handles embedded integer sequences intelligently:
file1.txt $\rightarrow$ file10.txt $\rightarrow$ file2.txtfile1.txt $\rightarrow$ file2.txt $\rightarrow$ file10.txtIn modern JavaScript, natural sorting is executed using String.prototype.localeCompare with { numeric: true }.
JavaScript
function sortLines(rawText, options = { reverse: false, natural: true }) {
const lines = rawText.split(/\r?\n/);
lines.sort((a, b) => {
return a.localeCompare(b, undefined, {
numeric: options.natural,
sensitivity: 'base'
});
});
if (options.reverse) {
lines.reverse();
}
return lines.join('\n');
}
Python
import re
def natural_sort_key(s: str):
# Splits string into alternating text and numeric chunks
return [int(text) if text.isdigit() else text.lower() for text in re.split(r'(\d+)', s)]
def sort_lines(text: str) -> str:
lines = text.splitlines()
lines.sort(key=natural_sort_key)
return "\n".join(lines)
รค sorted with a or after z). The tool uses standard locale collation to preserve national typographic conventions.Array.prototype.sort() is guaranteed to be stable, meaning items with identical sorting keys retain their original relative positions.This website and utility are provided for general informational, calculation, and educational purposes only. Always verify critical computation outputs prior to professional deployment or financial use. See our Terms of Service and Privacy Policy for full governance details.