Sort Text Lines
Sort text lines alphabetically or numerically.
100% client-side processing
Input lines: 5
Output lines: 0
Sort Text Lines takes an unordered block of text and returns the same lines in a chosen order. It supports three sorting modes, each with an ascending or descending direction, and runs entirely in the browser, so pasting a client list or a column of internal IDs does not send the data anywhere.
How to Use
- Paste or type your text into the input area, one entry per line.
- Pick a mode: alphabetical, numerical, or length.
- Pick a direction: ascending or descending.
- Adjust the case, whitespace, and empty-line options if the defaults do not fit.
- Copy the sorted output from the result pane.
Line endings are normalised first, so a file saved with Windows CRLF endings and one saved with Unix LF endings behave identically.
The Three Sort Modes
Alphabetical compares lines with localeCompare, which is locale-aware and, by default, case-insensitive. This is not the same as sorting by raw character code: it means accented letters, punctuation, and case are ordered the way a reader of the active locale expects.
Numerical parses the leading number of each line with parseFloat and sorts by value. Lines with no leading number are assigned a sort value of infinity, so in ascending order they sit at the bottom and in descending order they float to the top.
Length sorts by character count of the trimmed line, which is handy for finding outliers in a list of keywords or captions.
Worked Examples
Alphabetical, ascending, case-insensitive:
banana
Apple
cherry
becomes
Apple
banana
cherry
Numerical, ascending:
100
7
42
becomes 7, 42, 100. Note that alphabetical mode would produce 100, 42, 7, because the character 1 sorts before 4 and 7.
Length, ascending:
fig
apple
kiwi
becomes fig (3), kiwi (4), apple (5).
Numerical with mixed lines:
3 cats
12 dogs
no number here
becomes 3 cats, 12 dogs, then no number here. The third line has no leading digit, so it is treated as infinite and stays at the end.
Options That Change the Result
| Option | Default | Effect when changed |
|---|---|---|
| Direction | Ascending | Reverses the comparison, so Z-A, largest-to-smallest, or longest-first |
| Case sensitive | Off | Treats Apple and apple as distinct keys |
| Trim whitespace | On | When off, leading spaces count toward length and affect comparisons |
| Remove empty lines | On | When off, blank lines are kept and sorted as empty strings, landing first in ascending order |
Because trimming is on by default, a line of kiwi is measured as 4 characters, not 7. Turning trimming off changes both the length ranking and any alphabetical comparison that involved indentation.
Common Mistakes
- Expecting
Item 42to sort as 42. It will not; the prefix hides the number. Either move the number to the front or strip the prefix. - Forgetting that blank lines disappear. If you are trying to preserve spacing between groups, disable empty-line removal.
- Mixing numeric formats.
1,000,1.5, and1 000do not parse the way you might expect, and entries that fail to parse silently sink to the bottom. - Assuming code-point order. Alphabetical mode uses locale collation, so punctuation and case do not fall where a raw ASCII comparison would put them.
- Overlooking the reported line count. It reflects the post-removal count, which is a useful check that nothing was dropped unexpectedly.
Sorting Numbers in Real Data
Numerical mode is only as good as what parseFloat can read, and that function stops at the first character it cannot include in a number. The practical consequences are worth knowing before you trust a sort:
| Line | parseFloat reads | Sorts as |
|---|---|---|
3 cats | 3 | 3 |
-12 | -12 | -12 |
2.5 | 2.5 | 2.5 |
1e3 | 1000 | 1000 |
1,000 | 1 | 1 |
$5.99 | (fails) | infinity |
v2 | (fails) | infinity |
2.10 | 2.1 | 2.1 |
The last row is the trap for software teams. Semantic version strings such as 2.10 and 2.9 both look numeric, but the parser reads the first float only, so 2.10 becomes 2.1 and sorts below 2.9. For version numbers, split the components and sort each field, or zero-pad them to 2.10 versus 2.09 so a plain sort happens to work.
Practical Tips
- If your list looks like
Item 42, rearrange it to42 Itembefore sorting, or use a find-and-replace to move the number to the front. - Duplicate lines survive the sort and sit next to each other because the sort is stable. Remove duplicates first if you want a clean list.
- Case-insensitive alphabetical mode is the right default for names and words. Switch on case sensitivity only when the distinction genuinely matters, such as sorting a list of programming identifiers.
- Trimming is on by default, which is usually what you want, but turn it off if indentation is meaningful, such as when sorting code or nested outlines.
- Compare the reported line count with your input to catch lines silently dropped as empty or whitespace-only.
Real-World Scenarios
- Cleaning an exported CSV column of names or IDs before re-importing it.
- Sorting a keyword list by length to see which search terms are shortest and easiest to target.
- Ordering numbered log lines or test cases so the output is readable.
- Preparing a bibliography or glossary where entries must be alphabetical regardless of capitalisation.
- Ranking scores or dimensions by value when the numbers are at the start of each line.
Related Tools
Frequently Asked Questions
Does numerical sorting find a number anywhere in the line?
No. It calls parseFloat on the whole line, which reads a number only from the very start. A line like 42 items sorts as 42, but Item 42 has no leading number, so it is treated as non-numeric and given the value infinity. In ascending order those lines sink to the bottom; in descending order they rise to the top. Put the number first, or strip the prefix before sorting.
Is alphabetical sorting case-sensitive?
Not by default. Alphabetical mode lowercases both lines before comparing them, so Apple, apple, and APPLE are treated as equal keys. Turning on the case-sensitive option compares the original strings instead, which separates them and makes the ordering depend on the active locale's collation rules.
What happens to blank lines and leading spaces?
By default, each line is trimmed and empty lines are removed before sorting. That means a trailing newline or a line containing only spaces disappears from the output, and the reported line count can be lower than the number of lines you pasted. Both behaviours can be switched off.
Can I sort values like 1,000 or $5.99?
Not reliably. parseFloat stops at the first character that is not part of a number, so 1,000 is read as 1 and $5.99 is read as not-a-number and pushed to the end. Convert such values to plain numbers such as 1000 and 5.99 first, or the ordering will be wrong.
Are equal lines kept in their original order?
Yes. The sort is stable, so when two lines compare equal, such as Apple and apple in the default case-insensitive mode, they stay in the order you pasted them. Non-numeric lines in numerical mode are a special case: they all compare equal, so they also retain their input order at the bottom of the list.
What exactly does length mode count?
The number of characters in each trimmed line, including spaces inside the line. fig is 3, kiwi is 4, and apple is 5, so ascending length sorts them in exactly that order. It does not count words or bytes, so a line of five CJK characters counts as 5 even though it may be 15 bytes in UTF-8.
Reviewed by the HandyTools Hub team · Last updated