Sort Text Lines Alphabetically
Organize your text with our alphabetical line sorter. Whether you're arranging names, organizing lists, or sorting data exports, this tool provides instant A-Z (or Z-A) sorting with options for case sensitivity, numeric awareness, and locale-specific ordering.
Sorting Options
| Sort Type | Example Result | Best For | A-Z (Ascending)Apple, Banana, CherryStandard alphabetical Z-A (Descending)Cherry, Banana, AppleReverse order Case Insensitiveapple, Apple, APPLE togetherMixed case lists Natural/Numericfile1, file2, file10Numbered items | Locale-Aware | á after a, ñ after n | International text |
Text Line Sorting Algorithm
``javascript
function sortLinesAlphabetically(text, options = {}) {
const {
order = 'asc',
caseSensitive = false,
naturalSort = false,
locale = 'en'
} = options;
let lines = text.split('\n').filter(line => line.trim());
if (naturalSort) {
// Natural sorting: file1, file2, file10 (not file1, file10, file2)
lines.sort((a, b) => a.localeCompare(b, locale, { numeric: true, sensitivity: 'base' }));
} else {
lines.sort((a, b) => {
const compareA = caseSensitive ? a : a.toLowerCase();
const compareB = caseSensitive ? b : b.toLowerCase();
return compareA.localeCompare(compareB, locale);
});
}
if (order === 'desc') lines.reverse();
return {
sortedLines: lines.length,
result: lines.join('\n')
};
}
``
Natural vs. Lexicographic Sorting
Standard alphabetical sorting treats numbers as text: "file10" comes before "file2" because "1" < "2" character-by-character. Natural sorting understands numbers: "file2" correctly comes before "file10". Enable natural sorting for filenames, version numbers, or any list with numeric components.
International Sorting
Different languages have different sorting rules. In Spanish, "ñ" sorts after "n". In Swedish, "ö" comes at the end of the alphabet. Our locale-aware sorting respects these conventions. Select your locale for accurate international text sorting.