Installation
pip install cn-sort
Requires Python 3.6+. Dependencies (pypinyin, jieba) are installed automatically.
Quick start
from cn_sort import sort_text_list, Mode
# Sort by Pinyin (default)
sort_text_list(['唯依', '唯衣', '唯一', '啊'])
# → ['啊', '唯一', '唯衣', '唯依']
# Sort by stroke order
sort_text_list(['三', '二', '一', '一二'], mode=Mode.BIHUA)
# → ['一', '一二', '二', '三']
# Polyphonic characters handled by context
sort_text_list(['重要', '重庆'])
# → ['重庆', '重要'] (chóng < zhòng)
# Mixed Chinese / Latin
sort_text_list(['中国', 'abc', '啊'])
# → ['abc', '啊', '中国']
# Million-scale: multiprocess kicks in automatically
sort_text_list(big_list, threshold=100_000)
Features
- Two modes —
Mode.PINYIN(pinyin + stroke tiebreaker) orMode.BIHUA(stroke order only) - Polyphonic characters —
pypinyinuses surrounding context to pick the correct reading - Scales to 1M+ words — auto-switches to a multiprocess producer-consumer pipeline above
threshold - Mixed content — Latin / numeric / punctuation sorts before CJK by default
- Zero config — no extra data downloads; priority table ships inside the package
API
sort_text_list(text_list, *, freeze=False, threshold=100_000, mode=Mode.PINYIN) → list[str]
| Parameter | Type | Default | Description |
|---|---|---|---|
text_list |
list[str] |
required | Words to sort |
mode |
Mode |
Mode.PINYIN |
Sorting mode |
threshold |
int |
100_000 |
Switch to multiprocess above this count |
freeze |
bool |
False |
Set True when calling outside if __name__ == '__main__' on Windows |
Mode
| Value | Behaviour |
|---|---|
Mode.PINYIN |
Sort by Pinyin reading; stroke order breaks ties |
Mode.BIHUA |
Sort by stroke count only |
set_stdout_level(level: str) → bool
Set console log verbosity. level ∈ {"DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"}.
How it works
cn_sort converts each word into a tuple of integer priorities, then applies LSD radix sort — sorting from the last character column to the first using Python's stable timsort.
Pinyin mode
- Priority table — 20 000+ characters pre-ranked by Pinyin + stroke order, stored in
all_word.jsonfor O(1) lookup. - Word → tuple — each character maps to a signature (e.g.
人_ren2) looked up in the table. - LSD radix sort —
operator.itemgettersorts each column in place; stable sort guarantees correct ordering. - Polyphonic chars —
pypinyinselects the right reading from word context automatically.
Stroke-order mode
Characters are ranked by stroke increment level instead of Pinyin signature.
Polyphonic character example
Priority table schema
Large-scale multiprocess pipeline
For lists larger than threshold, cn_sort spawns a producer-consumer process pool:
- N producer processes (CPU count − 1): segment with jieba, deduplicate, push to independent queues.
- 1 consumer process: collects tokens from all queues, builds a priority-tuple cache.
- Main process: reassembles segments, applies final radix sort.
Performance
| Scale | Time | Notes |
|---|---|---|
| 10 words | < 1 ms | first call loads the priority table (~40 ms); subsequent calls are instant |
| 10 000 words | ~20 ms | repeated words hit the pypinyin cache; near-instant on warm runs |
| 48 000 words | ~100 ms | single-process; cache makes repeated patterns essentially free |
| 1 000 000 words | ~2.7 s | single-process with numpy lexsort; multiprocess mode for highly diverse word sets |
What makes it fast:
- The 20 000-character priority table loads once and stays in memory for the lifetime of the process.
pypinyinresults are cached per word — sorting a list where many words share characters (common in practice) skips redundant pinyin lookups entirely.- The producer stage skips jieba entirely — words arrive pre-separated by
\n, so splitting by\nis equivalent at a fraction of the cost. - Final sort uses
numpy.lexsort(C-level multi-key sort) instead of repeated Pythonlist.sort()passes. - The multiprocess pipeline uses direct
multiprocessing.Queuewith batched sends — no Manager proxy, minimal IPC overhead.
The multiprocess path is most effective when the word list has many unique words (e.g. a real dictionary). For data with high repetition, single-process with the pypinyin cache is faster.
Contributing
git clone https://github.com/bmxbmx3/cn_sort.git
cd cn_sort
pip install pypinyin jieba
- Fork the repo and create a branch:
git checkout -b feat/your-feature - Make changes; add tests where applicable.
- Open a Pull Request.
Good first contributions: Traditional Chinese support, a pytest test suite, faster large-scale segmentation.
License
MIT © bmxbmx3
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cn_sort-0.14.0.tar.gz.
File metadata
- Download URL: cn_sort-0.14.0.tar.gz
- Upload date:
- Size: 473.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a97869e588e9025e5e56514da03d6ee59569c678b00377471c982acf5a29718d
|
|
| MD5 |
46412d50dacdab2cd90931cbee1cfe12
|
|
| BLAKE2b-256 |
1b5af163af0a7bb32e9b0b347de84c3ccc05ced199d327dc7ce6d5fde8dff687
|
File details
Details for the file cn_sort-0.14.0-py3-none-any.whl.
File metadata
- Download URL: cn_sort-0.14.0-py3-none-any.whl
- Upload date:
- Size: 496.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1be7dd55552c0bb5aad6ac8bf4c97e0ef30df6b99cfc8b640bdb04cd0e916c06
|
|
| MD5 |
9f1a3289ed31910deb16e8ce2f7649e8
|
|
| BLAKE2b-256 |
12fd2ecf8bd5f5e3dcf600b7c7299d40035b47c99f12f65bec53c81f9c2c677d
|