Line Sorter
按字母、长度、倒序或随机顺序对文本行进行排序
输入行
排序结果
什么是行排序?
行排序是根据特定规则对文本块中的行重新排列的过程,规则包括:字母顺序、倒序、行长度或随机打乱。在处理日志文件、CSV 数据、配置列表或任何顺序有意义的纯文本内容时,在线行排序是一项常见操作。该操作将文本按换行符拆分,对得到的数组应用比较函数,然后将排序后的行重新合并。
大多数编程语言默认使用字典序比较对字符串排序,即按 Unicode 码位比较字符。这意味着大写字母排在小写字母之前("Banana" 排在 "apple" 之前),数字排在字母之前。有时称为自然排序或排序规则的区域感知排序通过应用特定语言的规则来修正这一问题。JavaScript 的 localeCompare()、Python 的 locale.strxfrm() 以及带 LC_COLLATE 的 POSIX sort 命令均提供区域感知排序。
按长度排序适用于需要找出列表中最短或最长条目、识别日志输出中的异常值,或按复杂度组织条目的场景。倒序排序是对现有行顺序进行翻转,而非重新排列,这与 Z-A 字母排序不同。随机打乱为每一行分配一个随机排序键,每次运行都会产生不同的顺序。在同一输入上切换排序模式,比临时编写脚本更高效。
为什么使用这个行排序工具?
粘贴文本,选择排序模式,立即获得结果。无需命令行配置、脚本文件或包安装。
行排序工具使用场景
行排序模式参考
本工具支持六种排序模式。下表描述了每种模式、所用的比较方法,以及对输入列表(apple、banana、cherry、date、fig)的示例输出。
| 模式 | 说明 | JS 方法 | 示例输出 |
|---|---|---|---|
| A-Z | Alphabetical ascending | localeCompare() | apple, banana, cherry |
| Z-A | Alphabetical descending | localeCompare() reversed | cherry, banana, apple |
| Length (short) | Shortest line first | a.length - b.length | fig, date, apple, banana |
| Length (long) | Longest line first | b.length - a.length | banana, apple, date, fig |
| Reverse | Flip line order, no reordering | Array.reverse() | Last line becomes first |
| Random | Randomized comparator (biased) | Math.random() - 0.5 | Different every run |
幕后的排序算法
在 JavaScript 中调用 Array.sort() 时,V8 引擎(Chrome、Node.js)自 2019 年起使用 Timsort。其他运行时使用不同的算法。下表比较了各语言标准库中最常用的排序算法。对于本工具的行排序工作负载,所有算法在典型输入(10 万行以内)下均可在 1 毫秒内完成。
| 算法 | 使用方 | 时间复杂度 | 备注 |
|---|---|---|---|
| Timsort | Python, Java (Arrays.sort) | O(n log n) | Stable, fast on partially sorted data |
| Quicksort | C stdlib, V8 (older) | O(n log n) | In-place, unstable by default |
| Merge sort | Most stable-sort implementations | O(n log n) | Stable, predictable, uses extra memory |
| Introsort | C++ std::sort, .NET | O(n log n) | Hybrid: quicksort + heapsort fallback |
| Radix sort | Fixed-length keys, integers | O(nk) | Non-comparative, linear for short keys |
代码示例
在 JavaScript、Python、Go 和命令行中以编程方式对行进行排序。每个示例涵盖字母排序、按长度排序和倒序排序。
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')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
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
}# 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