Line Sorter
Sort lines alphabetically, by length, reverse or randomize line order
Input lines
Sorted lines
What Is Line Sorting?
Line sorting is the process of rearranging the lines of a text block according to a specific rule: alphabetical order, reverse order, line length, or a random shuffle. Sorting lines online is a common task when working with log files, CSV data, configuration lists, or any plain-text content where order matters. The operation splits text on newline characters, applies a comparison function to the resulting array, and joins the sorted lines back together.
Most programming languages sort strings using lexicographic comparison by default, which compares characters by their Unicode code points. This means uppercase letters sort before lowercase ("Banana" before "apple"), and digits sort before letters. Locale-aware sorting, sometimes called natural sort or collation, fixes this by applying language-specific rules. JavaScript's localeCompare(), Python's locale.strxfrm(), and the POSIX sort command with LC_COLLATE all provide locale-aware ordering.
Sorting by length is useful when you need to find the shortest or longest entries in a list, identify outliers in log output, or organize items by complexity. Reverse sorting flips the existing line order without reordering, which is different from Z-A alphabetical sorting. Random shuffling assigns each line a random sort key, producing a different order on every run. Switching between modes on the same input is faster than writing a one-off script.
Why Use This Line Sorter?
Paste your text, pick a sort mode, and get the result instantly. No command-line setup, no script files, no package installs.
Line Sorter Use Cases
Line Sort Modes Reference
This tool supports six sort modes. The table below describes each mode, the comparison method it uses, and a sample result for the input list: apple, banana, cherry, date, fig.
| Mode | Description | JS Method | Example Output |
|---|---|---|---|
| A-Z | Alphabetical ascending | localeCompare() | apple, banana, cherry |
| Z-A | Alphabetical descending | localeCompare() reversed | cherry, banana, apple |
| Length (short) | Shortest line first | a.length - b.length | fig, date, apple, banana |
| Length (long) | Longest line first | b.length - a.length | banana, apple, date, fig |
| Reverse | Flip line order, no reordering | Array.reverse() | Last line becomes first |
| Random | Randomized comparator (biased) | Math.random() - 0.5 | Different every run |
Sorting Algorithms Behind the Scenes
When you call Array.sort() in JavaScript, the V8 engine (Chrome, Node.js) uses Timsort since 2019. Other runtimes use different algorithms. The table below compares the most common sorting algorithms used in language standard libraries. All handle the line-sorting workloads in this tool in under a millisecond for typical inputs (under 100,000 lines).
| Algorithm | Used By | Time | Notes |
|---|---|---|---|
| Timsort | Python, Java (Arrays.sort) | O(n log n) | Stable, fast on partially sorted data |
| Quicksort | C stdlib, V8 (older) | O(n log n) | In-place, unstable by default |
| Merge sort | Most stable-sort implementations | O(n log n) | Stable, predictable, uses extra memory |
| Introsort | C++ std::sort, .NET | O(n log n) | Hybrid: quicksort + heapsort fallback |
| Radix sort | Fixed-length keys, integers | O(nk) | Non-comparative, linear for short keys |
Code Examples
Sort lines programmatically in JavaScript, Python, Go, and the command line. Each example covers alphabetical, length-based, and reverse sorting.
const text = `banana
apple
cherry
date
fig`
// Sort A-Z (locale-aware)
const az = text.split('\n').sort((a, b) => a.localeCompare(b)).join('\n')
// → "apple\nbanana\ncherry\ndate\nfig"
// Sort by line length, shortest first
const byLen = text.split('\n').sort((a, b) => a.length - b.length).join('\n')
// → "fig\ndate\napple\nbanana\ncherry"
// Reverse line order (no alphabetical sorting)
const reversed = text.split('\n').reverse().join('\n')
// → "fig\ndate\ncherry\napple\nbanana"
// Remove duplicates and sort
const unique = [...new Set(text.split('\n'))].sort().join('\n')text = """banana apple cherry date fig""" lines = text.splitlines() # Sort A-Z (case-insensitive) az = sorted(lines, key=str.lower) # → ['apple', 'banana', 'cherry', 'date', 'fig'] # Sort by line length by_len = sorted(lines, key=len) # → ['fig', 'date', 'apple', 'banana', 'cherry'] # Reverse original order rev = lines[::-1] # → ['fig', 'date', 'cherry', 'apple', 'banana'] # Shuffle randomly import random random.shuffle(lines) # modifies in place
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
text := "banana\napple\ncherry\ndate\nfig"
lines := strings.Split(text, "\n")
// Sort A-Z
sort.Strings(lines)
fmt.Println(strings.Join(lines, "\n"))
// → apple\nbanana\ncherry\ndate\nfig
// Sort by length
sort.Slice(lines, func(i, j int) bool {
return len(lines[i]) < len(lines[j])
})
fmt.Println(strings.Join(lines, "\n"))
// → fig\ndate\napple\nbanana\ncherry
}# Sort lines A-Z
sort file.txt
# Sort lines Z-A (reverse)
sort -r file.txt
# Sort numerically (first field)
sort -n data.txt
# Sort by line length (awk + sort + cut)
awk '{ print length, $0 }' file.txt | sort -n | cut -d' ' -f2-
# Shuffle lines randomly
shuf file.txt # GNU coreutils
sort -R file.txt # alternative (not truly uniform)
# Sort and remove duplicates
sort -u file.txt