Опубліковано: 2026-05-27
Sort Lines Alphabetically: VS Code, Terminal & Online
Sort lines alphabetically in VS Code, the terminal, or online — plus the case-sensitivity and locale gotchas that quietly change your results. Sort yours now.

To sort lines alphabetically, you've got three solid options: paste your list into an online sorter and click A→Z, run Sort Lines Ascending from the VS Code Command Palette, or pipe a file through sort in the terminal. All three work. The twist nobody mentions is that they don't always agree — case-sensitivity and locale settings quietly change the order you get back.
That's the part that bites you. You sort a list in VS Code, sort the "same" list on a colleague's machine with sort, and the two outputs differ. Neither is broken. They're just using different collation rules. Let's get the order you actually want, in whichever environment you're in.
The Fastest Path: Sort Online
For a one-off list, skip the tooling. Paste it into our Sort Lines — it runs 100% in your browser, zero data sent to any server — and pick Sort A→Z. Every newline is one item, so names, URLs, ES imports, log lines, or CSV rows all alphabetize in a single click.
Under the hood it uses JavaScript's String.prototype.localeCompare — locale-aware collation, not a naïve byte comparison. That's the same ICU-backed ordering your operating system uses, so accented characters and punctuation land where a human expects them, not dumped after z. A few specifics:
- Sort A→Z / Z→A — ascending or descending, your call.
- Case-sensitive toggle — off by default (dictionary order); flip it on to group capitals separately.
- By length — shortest-first or longest-first, handy for spotting outliers in a list.
- Shuffle — randomizes order with a Fisher-Yates shuffle (the unbiased one, O(n)), useful for sampling or breaking up a sorted bias.
One thing to know up front: this is a text sort, so numbers order lexicographically — 10 lands before 2, because it compares character by character. If your list is numeric, zero-pad it (02, 10) so text order and numeric order line up. For native numeric ordering you want the terminal's sort -n instead — covered below.
The sort is stable, too — lines that compare equal keep their original relative order, which matters when you sort by length and want ties left alone.
Sorting Lines in VS Code
VS Code has this built in — no extension needed for the basics. Select your lines (or don't, to sort the whole file), open the Command Palette with Cmd/Ctrl + Shift + P, type "Sort Lines", and choose Sort Lines Ascending or Sort Lines Descending.
Here's the limitation that trips people up: the built-in command is strictly case-sensitive, and it offers no numeric sort and no duplicate removal. So Banana sorts before apple every time, with no toggle to change it. This has been a long-standing request (microsoft/vscode#18315).
If you need case-insensitive order, numeric sort, or dedup-while-sorting, install the Tyriar "Sort lines" extension. It adds those modes and binds sorting to F9. Many devs pair sorting with deduplication — if that's you, the same workflow and edge cases are covered in our guide to removing duplicate lines in VS Code.
Sorting Lines in the Terminal
The Unix sort command is the workhorse, but its default behavior is locale-dependent — which is exactly why two machines disagree.
| Command | What it does |
|---|---|
sort file.txt | Sort lines using the current locale's collation (LC_COLLATE) |
LC_ALL=C sort file.txt | Sort in raw byte order (capitals before lowercase) — deterministic |
sort -f file.txt | Fold case — case-insensitive dictionary order |
sort -n file.txt | Numeric sort (so 10 comes after 9, not after 1) |
sort -r file.txt | Reverse (Z→A) |
sort -u file.txt | Sort and remove duplicates in one pass |
The headline gotcha: on a machine set to en_US.UTF-8, sort mostly ignores case and punctuation in its primary comparison; on a box running LC_ALL=C, it sorts by byte value, where every uppercase letter precedes every lowercase one. Same file, same command, different output. When you need a result that's identical everywhere — a checked-in fixture, a diffable manifest — pin it with LC_ALL=C sort.
PowerShell users have the equivalent in Get-Content file.txt | Sort-Object (add -Unique, -Descending, or -CaseSensitive as needed); it's case-insensitive by default, the opposite of LC_ALL=C.
The Real Reason Your Orders Differ: Case & Collation
This is the concept that ties all three tools together. There are two ways to compare two strings:
- Code-point order — compare raw character codes. In ASCII,
A–Zare 65–90 anda–zare 97–122, so every capital sorts before every lowercase letter.Zebra(90…) beatsapple(97…). This is whatLC_ALL=C sortand VS Code's built-in command do. - Locale-aware collation — fold case and weight characters the way a dictionary does: apple, Banana, cherry, Zebra. This is what
localeCompare(our tool),sort -f, andSort-Objectdo.
Here's the same four-item list — apple, Banana, cherry, Zebra — sorted both ways:
| # | Code-point order (LC_ALL=C, VS Code built-in) | Dictionary order (localeCompare, sort -f) |
|---|---|---|
| 1 | Banana | apple |
| 2 | Zebra | Banana |
| 3 | apple | cherry |
| 4 | cherry | Zebra |
See how Zebra jumps to the top on the left? That's the capital Z (code point 90) outranking the lowercase letters (97+) — the single most common "why is my sort wrong" moment.
Neither is "wrong" — but you have to know which one you're invoking, or you'll chase a phantom bug. If your sorted list looks like it put all the capitalized entries in a clump at the top, you're getting code-point order and probably want case-insensitive instead.
| Method | Case-insensitive? | Numeric sort? | Dedup? | Reverse? | Offline / private |
|---|---|---|---|---|---|
| Online (Sort Lines) | Toggle | — | via Remove Duplicates | Yes | Yes (in-browser) |
| VS Code built-in | No (always case-sensitive) | No | No | Yes | Yes |
| VS Code + Tyriar ext. | Yes | Yes | Yes | Yes | Yes |
Terminal sort | -f | -n | -u | -r | Yes |
PowerShell Sort-Object | Default | Auto on numbers | -Unique | -Descending | Yes |
Clean Before You Sort
One last trap. Sorting is only as good as the lines you feed it, and pasted text is rarely clean. A trailing space, a non-breaking space (U+00A0) from a web copy, or a stray tab will sort an item to an unexpected spot — and will keep "duplicate" lines from collapsing because they aren't byte-identical.
Run the list through our Remove Spaces tool first to strip trailing whitespace and exotic Unicode spaces; the full list of contaminants that hitchhike on pasted text is broken down in how to remove extra spaces from text online. Then sort. Then, if you need uniqueness, run it through Remove Duplicates. Clean → sort → dedup is the reliable pipeline.
Frequently Asked Questions
Does sorting work with non-English characters?
Yes. The online tool's localeCompare is Unicode-aware, so Cyrillic, accented Latin, and other scripts collate sensibly rather than being shoved past z. In the terminal, accurate non-ASCII ordering depends on a UTF-8 locale (e.g. LC_COLLATE=en_US.UTF-8); under LC_ALL=C you get byte order, which splits multibyte characters in ways that rarely match human expectations.
Can I sort a huge list without freezing my browser?
For typical lists — even tens of thousands of lines — an in-browser sort resolves in well under a second, because Array.prototype.sort is highly optimized in V8. The cost is the comparison count (O(n log n)), not network latency, since nothing is uploaded. Genuinely massive files (hundreds of MB) are still better handled by the streaming sort command on disk.
Why isn't my numeric list sorting correctly?
Alphabetical sorting is text-based, so 10 sorts before 2 (it compares 1 then 0 against 2). That's not a bug — it's lexicographic order. For true numeric order, use sort -n in the terminal, or PowerShell's Sort-Object, which detects numbers automatically. Within a text-only sorter, zero-pad your numbers (02, 10) so lexicographic and numeric order coincide.
Sort It, Then Move On
Pick the tool that matches the moment: the Command Palette when you're already in VS Code, sort when you're in a shell and need it scripted, and the Sort Lines tool when you just want a list alphabetized without thinking about locales or installing anything. Whatever you choose, decide up front whether you want case-sensitive or dictionary order — that single choice explains almost every "weird" sort result you'll ever hit.
