Sort Lines
Sort text lines alphabetically in ascending (A-Z) or descending (Z-A) order with case-aware comparison.
Back to all tools on ToolForge
Input
Result
About Sort Lines
This sort lines tool arranges text lines in alphabetical order using JavaScript's locale-aware string comparison. It supports both ascending (A to Z) and descending (Z to A) sorting, handling Unicode characters, accented letters, and international text correctly through the Intl.Collator API.
It is useful for organizing keyword lists, alphabetizing names, sorting code imports, arranging configuration entries, cleaning data exports, preparing documentation indexes, ordering bibliographies, and any task requiring line-by-line alphabetical organization.
Sorting Algorithm Overview
This tool uses JavaScript's built-in sorting with locale-aware comparison:
Algorithm: Timsort (JavaScript engine dependent) Complexity: O(n log n) average and worst case Stability: Stable (preserves equal element order) Space: O(n) auxiliary space Comparison Method: String.localeCompare() with locale sensitivity - Handles accented characters (é, ñ, ü) - Respects language-specific collation rules - Supports case-sensitive and case-insensitive modes Sort Process: 1. Split input text on line breaks (/\r?\n/) 2. Apply stable sort using localeCompare 3. Join sorted lines with newline characters 4. Output result preserving original line content Example (ascending): Input: banana, Apple, cherry, Apple Output: Apple, Apple, banana, cherry
Sort Order Comparison
Original Input: Zebra apple Banana cherry Apple Ascending (A-Z): Apple Banana Zebra apple cherry (Uppercase first due to Unicode code points) Descending (Z-A): cherry apple Zebra Banana Apple Case-Insensitive Ascending: Apple apple Banana cherry Zebra (More natural alphabetical order)
Unicode Code Point Reference
Understanding why sorting behaves certain ways requires knowing Unicode values:
| Character Type | Code Point Range | Example |
|---|---|---|
| Numbers (0-9) | U+0030 - U+0039 | 0=48, 9=57 |
| Uppercase (A-Z) | U+0041 - U+005A | A=65, Z=90 |
| Lowercase (a-z) | U+0061 - U+007A | a=97, z=122 |
| Latin Extended | U+00C0 - U+024F | À=192, ñ=241, ü=252 |
| Greek | U+0370 - U+03FF | Α=913, ω=969 |
| Cyrillic | U+0400 - U+04FF | А=1040, я=1103 |
Common Use Cases
| Domain | Use Case | Example |
|---|---|---|
| Programming | Sort imports/using statements | Organize Python imports, JS modules |
| Data Processing | Clean CSV/export data | Sort customer lists, product catalogs |
| Documentation | Create indexes/glossaries | Alphabetize terms, API references |
| SEO | Organize keyword lists | Sort keywords, meta tags, backlinks |
| Research | Bibliography organization | Sort references, author lists |
| System Admin | Config file management | Sort hosts files, package lists |
Sorting Examples
Example 1: Simple Word List Input: dog cat bird elephant ant Output (A-Z): ant bird cat dog elephant Example 2: Names with Mixed Case Input: John alice Bob Charlie david Output (A-Z, case-sensitive): Bob Charlie John alice david Output (case-insensitive): alice Bob Charlie david John Example 3: Version Numbers Input: v10 v2 v1 v20 Output (lexicographic): v1 v10 v2 v20 Output (natural/numeric sort): v1 v2 v10 v20 Example 4: File Names Input: report_2024.pdf report_2023.pdf report_2025.pdf notes.txt Output (A-Z): notes.txt report_2023.pdf report_2024.pdf report_2025.pdf
Command Line Equivalents
Unix/Linux (sort command): # Basic ascending sort sort file.txt # Descending sort sort -r file.txt # Case-insensitive sort sort -f file.txt # Remove duplicates while sorting sort file.txt | uniq # Sort numerically sort -n file.txt # Reverse numeric sort sort -rn file.txt # Sort by specific column sort -k2 file.txt Windows PowerShell: # Sort lines Get-Content file.txt | Sort-Object # Descending Get-Content file.txt | Sort-Object -Descending # Case-insensitive Get-Content file.txt | Sort-Object -CaseSensitive:$false # Unique sorted Get-Content file.txt | Sort-Object -Unique macOS (BSD sort): # Same as Linux, but use -g for general numeric sort -g file.txt
Programming Language Sorting
JavaScript:
// Basic sort
lines.sort()
// Locale-aware sort
lines.sort((a, b) => a.localeCompare(b))
// Case-insensitive sort
lines.sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase())
)
// Natural/numeric sort
lines.sort((a, b) =>
a.localeCompare(b, undefined, { numeric: true })
)
Python:
# Basic sort
sorted(lines)
# Case-insensitive
sorted(lines, key=str.lower)
# Reverse sort
sorted(lines, reverse=True)
# Remove duplicates
sorted(set(lines))
Java:
// Using Collections
Collections.sort(lines)
// Case-insensitive
lines.sort(String.CASE_INSENSITIVE_ORDER)
// Using Stream API
lines.stream()
.sorted()
.collect(Collectors.toList())
Special Sorting Considerations
- Empty lines: Empty lines typically sort to the beginning. Filter them first if unwanted: lines.filter(l => l.trim())
- Whitespace: Leading/trailing spaces affect sort order. Trim before sorting if consistency matters.
- Numbers in text: "Item 10" sorts before "Item 2" lexicographically. Use natural sort for numeric ordering.
- Accented characters: é, è, ê may sort after z or with e depending on locale. Use locale-aware comparison.
- Performance: Sorting 10,000+ lines may take noticeable time. Consider chunking for very large inputs.
- Duplicates: This tool preserves duplicates. Use deduplication if unique entries are needed.
Sort Order for Different Locales
Locale-Specific Collation Examples: German (de-DE): ä, ö, ü typically sorted as ae, oe, ue Müller sorts as if spelled Mueller Swedish (sv-SE): å, ä, ö are separate letters at alphabet end Order: ... x, y, z, å, ä, ö Spanish (es-ES): ñ is separate letter after n ch and ll were formerly separate (pre-1994) French (fr-FR): Accents ignored in primary sorting é, è, ê, ë all sort with e Danish/Norwegian (da-DK, no-NO): æ, ø, å at alphabet end Order: ... x, y, z, æ, ø, å Icelandic (is-IS): þ (thorn) and ð (eth) are letters þ sorts after z, ð sorts with d
Frequently Asked Questions
- How does alphabetical sorting work?
- Alphabetical sorting compares strings character by character using Unicode code point values. Uppercase letters (A-Z, codes 65-90) come before lowercase (a-z, codes 97-122) in ASCII sorting. The localeCompare() method handles language-specific rules, accented characters, and case-insensitive comparisons for more natural sorting.
- What is the difference between ascending and descending sort?
- Ascending sort (A-Z) arranges items from beginning to end of the alphabet or smallest to largest numerically. Descending sort (Z-A) reverses this order. Ascending is standard for dictionaries and indexes; descending is useful for reverse chronological order, top-N lists, or finding Z-items quickly.
- Why do uppercase and lowercase affect sort order?
- In ASCII/Unicode, uppercase letters have lower code points than lowercase (A=65, a=97). Simple sorting places 'Zebra' before 'apple'. Case-insensitive sorting treats 'A' and 'a' as equivalent, producing more natural results. For consistent sorting, normalize case or use locale-aware comparison.
- How do I remove duplicate lines while sorting?
- After sorting, filter duplicates by keeping only unique lines. In JavaScript: lines.filter((line, index) => lines.indexOf(line) === index). In Unix: sort file.txt | uniq. In Excel: Remove Duplicates feature. This is useful for cleaning imported lists, deduplicating tags, or consolidating data exports.
- What sorting algorithm is used?
- Modern JavaScript engines use Timsort (hybrid of merge sort and insertion sort) with O(n log n) average complexity. Timsort is stable (preserves equal element order), adaptive (fast on partially sorted data), and efficient on real-world datasets. It was developed by Tim Peters in 2002 for Python.
- How do I sort numbers within text lines?
- Numeric sorting differs from lexicographic sorting: '10' comes before '2' alphabetically. For proper numeric order, extract and compare numbers as values, not strings. Use natural sort algorithms or localeCompare with numeric: true option. This handles version numbers (v1, v2, v10) and mixed alphanumeric data correctly.