Sort text lines

Sort lines alphabetically with case and trim options for comparison.

Overview

Sorting a text list sounds simple until you ask yourself: does café come before or after caret? The answer depends on the locale collation rules — and they are more complicated than they appear. The Unicode specification includes the DUCET (Default Unicode Collation Element Table), a table of over 7,000 entries that defines the weight of each character for sorting purposes across multiple languages.

The practical problem surfaces when sorting strings that contain accents or special characters. Pure lexicographic sorting by Unicode code point places é after z, which makes no sense in any human language. The correct solution is to use localeCompare with the appropriate locale and sensitivity options. On the web, the Intl.Collator API standardized by ECMAScript handles this — and it makes the difference between a list that feels right to the user and one that feels broken.

Sorting algorithms have a rich history. Bubble sort is the most famous for teaching purposes and the least used in production. Quicksort, proposed by Tony Hoare in 1959, dominated decades with its average O(n log n) performance. Timsort, created by Tim Peters in 2002 for Python and later adopted by Java and the V8 JavaScript engine, combines merge sort with insertion sort and exploits already-sorted runs in the data — a piece of practical genius. The stability property — lines with equal values keeping their original relative order — matters when sorting by multiple criteria in separate passes.

Technical deep dive

Sorting and accents: the problem nobody notices until it breaks

  • Using JavaScript's native sort() without localeCompare places 'é' after 'z' because it compares by Unicode values, not by language rules. In a product or name list, the result looks completely broken.
  • Intl.Collator with the correct locale handles accented characters properly using collation rules defined by the Unicode CLDR.
  • Accent sensitivity (sensitivity: 'base' vs 'accent') determines whether 'e' and 'é' are considered equal for comparison purposes or treated as distinct letters.
  • Natural sort interprets numbers inside strings as numeric values: item10 comes after item9, not before. This requires the numeric: true option in Intl.Collator.

Practical use cases

  • Alphabetizing lists of names, countries, cities, and products before displaying to end users or including in documents.
  • Sorting keys in translation files (i18n) to make review easier and avoid hidden duplicates in YAML or JSON files.
  • Organizing imports in code files consistently, reducing noise in code reviews.
  • Preparing text data before applying binary search, which requires the list to be sorted.
  • Sorting CSV lines by a specific column before comparing against another version of the file.

Tool guide

  • What you are working with Text split into lines.

  • What the tool does Sorts lines alphabetically (A→Z or Z→A), with case and trim options for the sort key. Stable sort.

  • Why use it Organise lists, compare sets visually, prepare files for cleaner diffs.

Code Snippets

Locale-aware sort in JavaScript
const lines = text.split('\n').filter(Boolean);
lines.sort((a, b) =>
  a.localeCompare(b, 'en-US', { sensitivity: 'variant', numeric: true })
);
const result = lines.join('\n');
Reverse sort (Z → A)
lines.sort((a, b) =>
  b.localeCompare(a, 'en-US', { sensitivity: 'variant' })
);

Input

banana
apple
cherry → apple
banana
cherry

FAQ

What is this tool for?

It runs fully in your browser: useful to validate, format, or convert data in everyday development.

Are my inputs sent to a server?

Processing happens locally with JavaScript. We do not store what you paste into the text areas.

Can I use this for real production data?

Use at your own risk. For secrets (passwords, tokens), prefer controlled environments and your company policies. And always review the generated contents. Never trust blindly things you see on the internet.