Sort lines numerically
item 20, item 3, item 100 and item 11 come back as item 3, item 11, item 20, item 100, ordered by value instead of by characters, which would put 100 before 20. Choose largest first if you prefer. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Text
- item 20 item 3 item 100 item 11
- Text
- item 3 item 11 item 20 item 100
- Lines
- item 3, item 11, item 20, item 100
- Values
- 3, 11, 20, 100
- Quantity
- 4
Sort lines numerically when alphabetical order gives the wrong answer. A text sort places 100 before 20 because it compares characters, while this capability extracts the first number from every line and compares its actual numeric value. It handles signed integers, decimals, and scientific notation, preserves each complete line, and keeps the original order when two numeric keys are equal. Use ascending or descending order and receive both copy-ready text and structured arrays. Every line must contain a valid finite number, so malformed data produces a clear error instead of an unreliable partial result.
Why numeric line sorting differs from alphabetical sorting
Ordinary text sorting compares characters from left to right. That rule is useful for words, but it produces surprising results for numbered records: a line containing 100 can appear before a line containing 20 because the character 1 precedes 2. Numeric line sorting solves that specific problem by finding the first decimal number in every line, converting it to a finite numeric value, and using that value as the sort key.
The rest of each line stays untouched, so labels, units, punctuation, and spacing travel with their number. Signed values work as expected, which places -12 below -3 in ascending order. Decimal forms such as 0.5 and .75 are accepted, as are scientific values such as 2e3 or -4.2E-2.
A number must stand apart from letters, digits, and underscores; digits embedded inside an identifier such as version2beta are not silently interpreted as a measurement. If a line contains several numbers, the first one is deliberately used. This explicit rule makes invoice rows, rankings, timed log entries, and numbered labels predictable without asking the sorter to guess which field matters.
Lines with identical numeric keys retain their original relative order, providing a stable result that does not shuffle tied records between runs.
How to prepare input and understand the result
Provide the multiline source in the required text field. Line endings from Unix, Windows, and older carriage-return files are recognized, then the returned joined text uses a single line-feed separator for consistent copying and downstream processing. Every line, including a blank line created by a leading, trailing, or repeated newline, must contain a number.
This strict validation protects automated pipelines: instead of placing an unparseable row at an arbitrary end of the list, the capability reports the one-based line number that needs attention. Choose asc, the default, to arrange values from smallest to largest, or desc to reverse that numeric order. Original line spelling and whitespace are preserved exactly except for normalization of separators between lines.
The algorithm does not remove duplicates, trim labels, interpret thousands separators, or select the second number on a line. Remove grouping commas before submitting values such as 1,200, because that spelling would be read as the first standalone number 1 rather than twelve hundred.
Reliable use in browser tools and automated workflows
Use the browser tool for quick jobs such as reordering chapter labels, priority queues, benchmark output, price lists, or log messages that already include a numeric counter. A useful workflow validates or normalizes the source format first, sends the resulting lines for numeric sorting, and then consumes either the joined text or the structured lines array. Because the implementation uses no network, randomness, locale-sensitive collation, or current date, identical input and options always produce identical output in the browser and on the server.
Numeric ties are stable even in descending mode: changing direction reverses the comparison of numeric values but does not reverse records that share the same key. This tool intentionally focuses on one clear operation. It does not sort tabular columns, evaluate arithmetic expressions, recognize currency formatting, or perform natural sorting across mixed words and multiple digit groups.
Those tasks require different parsing rules. Here, rejecting a line without a finite first numeric literal is safer than inventing an ordering that could conceal missing data in an automated report.
What you can do with it
Order numbered labels correctly
Arrange item 2, item 11, and item 100 by their actual numbers while preserving every label exactly.
Rank measurements and benchmark rows
Sort lines containing negative values, decimals, or scientific notation for a reproducible smallest-to-largest or largest-to-smallest report.
FAQ
Which number is used when a line contains several numbers?
The first standalone numeric literal on the line is the sort key. Later numbers remain part of the unchanged line text but do not affect its position.
Are negative numbers and decimals supported?
Yes. Signed integers, decimal values such as -3.5 or .75, and scientific notation such as 2e3 are supported when they are finite.
What happens when two lines contain the same value?
The sort is stable, so tied lines remain in the same relative order in which they appeared in the input.
Why do I get an error pointing to one line?
Every line must contain a number, and that line has none. A blank line also counts, including the empty one left by a final line break. The message names the line number so you can fix or remove it.
Why isn't a number inside a word like version2beta used?
A number only counts when it stands on its own, not glued to letters, so digits inside a code such as version2beta are not treated as a value. If that line has no other number, you will get an error for it.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/str/sort-lines-numeric \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"text":"item 20\nitem 3\nitem 100\nitem 11"}'const res = await fetch("https://api.kit.forhosting.com/str/sort-lines-numeric", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"text": "item 20\nitem 3\nitem 100\nitem 11"
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/str/sort-lines-numeric",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"text": "item 20\nitem 3\nitem 100\nitem 11"
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/str/sort-lines-numeric", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"text":"item 20\\nitem 3\\nitem 100\\nitem 11"}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"text":"item 20\nitem 3\nitem 100\nitem 11"}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/str/sort-lines-numeric", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"text": "item 20\nitem 3\nitem 100\nitem 11"
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "str.sort_lines_numeric",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
How to prepare input and understand the result
The response contains text for direct clipboard or file use, lines for callers that prefer a JSON array, and values showing the extracted numeric keys in their final order. It also returns count and the effective order, making it easy to verify a transformation without parsing the text again.
Integration use cases
Normalize deterministic test fixtures
Create stable numeric ordering for generated logs and fixtures while retaining the original order of rows with equal keys.
Technical questions
What if one line has no number?
The request fails with invalid_input and identifies the one-based line number. No partial sorted result is returned.
How much does numeric line sorting cost?
The browser tool is free. Each successful API request costs $0.002; invalid input is rejected without a successful result.