Line Sorter

Sort lines alphabetically, by length, reverse or randomize line order

Try an example

Input lines

Sorted lines

Runs locally · Safe to paste secrets
Sorted lines will appear here…

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.

Instant Sorting
Results appear as you type or paste. Switch between six sort modes and compare output without re-running anything.
🔒
Privacy-First Processing
All sorting runs in your browser with JavaScript. Your text stays on your device. Nothing is uploaded to a server or logged.
🔀
Six Sort Modes
A-Z, Z-A, shortest first, longest first, reverse order, and random shuffle. One input covers the most common line-ordering tasks.
📋
No Account Required
Open the page and start sorting. No signup, no extension, no desktop app. Works on any device with a modern browser.

Line Sorter Use Cases

Frontend Development
Sort CSS class lists, import statements, or i18n translation keys alphabetically. Consistent ordering reduces merge conflicts in version control and makes code reviews faster.
Backend Engineering
Order dependency lists in package.json, requirements.txt, or go.mod before committing. Sort SQL column names when building CREATE TABLE statements or comparing schema diffs.
DevOps and Infrastructure
Sort environment variable names in .env files, Kubernetes ConfigMap entries, or Terraform variable blocks. Alphabetical order lets you spot duplicates and missing values during review.
QA and Test Automation
Sort test output logs by timestamp or message to isolate failures quickly. Randomize test input data to verify that application behavior does not depend on insertion order.
Data Engineering
Order CSV headers or column lists before writing schema migration scripts. Sort data sample lines by length to find truncated rows or unusually long values in a data pipeline.
Students and Learners
Sort vocabulary lists, bibliography entries, or study notes alphabetically. Shuffle flashcard lines for randomized review sessions without installing a separate app.

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.

ModeDescriptionJS MethodExample Output
A-ZAlphabetical ascendinglocaleCompare()apple, banana, cherry
Z-AAlphabetical descendinglocaleCompare() reversedcherry, banana, apple
Length (short)Shortest line firsta.length - b.lengthfig, date, apple, banana
Length (long)Longest line firstb.length - a.lengthbanana, apple, date, fig
ReverseFlip line order, no reorderingArray.reverse()Last line becomes first
RandomRandomized comparator (biased)Math.random() - 0.5Different 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).

AlgorithmUsed ByTimeNotes
TimsortPython, Java (Arrays.sort)O(n log n)Stable, fast on partially sorted data
QuicksortC stdlib, V8 (older)O(n log n)In-place, unstable by default
Merge sortMost stable-sort implementationsO(n log n)Stable, predictable, uses extra memory
IntrosortC++ std::sort, .NETO(n log n)Hybrid: quicksort + heapsort fallback
Radix sortFixed-length keys, integersO(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.

JavaScript
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')
Python
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
Go
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
}
CLI (bash)
# 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

Frequently Asked Questions

What is the difference between A-Z sort and natural sort?
A-Z sort (lexicographic) compares characters by Unicode code point. This means "item10" sorts before "item2" because the character '1' has a lower code point than '2'. Natural sort treats embedded numbers as numeric values, so "item2" comes before "item10". This tool uses locale-aware lexicographic sorting via localeCompare(), which handles accented characters correctly but does not perform natural sort on numbers.
Is my text sent to a server when I sort lines?
No. All sorting happens in your browser using JavaScript's Array.sort() method. The text never leaves your device. You can verify this by opening the browser's Network tab in DevTools and confirming that no requests are made when you paste and sort text.
How many lines can this tool handle?
The tool works well with tens of thousands of lines. JavaScript's Timsort implementation in V8 handles 100,000 lines in under 100 milliseconds on modern hardware. For files larger than a few megabytes, a CLI tool like the Unix sort command is more efficient because it can use disk-based merge sort and multiple threads.
How does case-insensitive sorting work?
This tool uses String.localeCompare() with the { sensitivity: 'base' } option, which makes the comparison case-insensitive so that "Apple" and "apple" sort together rather than in separate groups. By default, localeCompare() is case-sensitive; the explicit option is required for case-insensitive behavior. If you need strict case-sensitive sorting where uppercase letters come first, the Unix sort command without the -f flag gives that behavior.
Can I sort lines and remove duplicates at the same time?
This tool sorts lines but does not remove duplicates. To remove duplicate lines, use the Duplicate Line Remover tool in the same category. You can sort your text here first, then paste the result into the duplicate remover for a clean, unique, sorted list.
What is the difference between reverse sort and Z-A sort?
Reverse sort flips the original line order: the last line becomes the first, the second-to-last becomes the second, and so on. No alphabetical comparison is involved. Z-A sort arranges lines in descending alphabetical order regardless of their original position. If your input is already sorted A-Z, reverse and Z-A produce the same output, but for unsorted input the results differ.
How do I sort lines by a specific column or field?
This tool sorts by the entire line content. To sort by a specific column (for example, the second field in a tab-separated file), use the Unix sort command with the -k flag: sort -t$'\t' -k2,2 file.txt. In Python, split each line and use a key function: sorted(lines, key=lambda x: x.split('\t')[1]).