Skip to main content

A blazing fast natural sorting library for Python

Project description

natsort-rs

🚀 A blazing fast natural sorting library for Python written in Rust 🦀

Installation

pip install natsort-rs

Usage

from natsort_rs import natsort

Sort a list of strings

items = ['item 1', 'item 10', 'item 3']
print(natsort(items))  
# ['item 1', 'item 3', 'item 10']

Sort case insensitively

items = ['Item 1', 'Item 3', 'item 2']
print(natsort(items, ignore_case=True))
# ['Item 1', 'item 2', 'Item 3']

Sort complex objects based on property

items = [
    {'name': 'item 1', 'id': 1},
    {'name': 'item 3', 'id': 3},
    {'name': 'item 2', 'id': 2}
]
print(natsort(items, key=lambda d: d['name']))
# [{'name': 'item 1', 'id': 1}, {'name': 'item 2', 'id': 2}, {'name': 'item 3', 'id': 3}]

Return the sorting indices

This can be helpful if you only want to get the sorted indices returned, that makes the performance-critical part useful for custom sorting use cases:

items = ['item 1', 'item 10', 'item 3']
print(natsort(items, return_indices=True))  
# [0, 2, 1]

Turn off multithreading

Multithreading is enabled by default and can be turned off by setting parallel = False.

Benchmark

No. of items Duration natsort [s] Duration natsort-rs [s] Relative speedup
10 0.00006 0.00000 16.8
100 0.00094 0.00002 44.3
1000 0.00281 0.00022 12.7
10000 0.02835 0.00262 10.8
100000 0.29712 0.03334 8.9
1000000 3.31207 0.45333 7.3

Execute benchmark.py to reproduce the results.

Credits

This Python module is build on top of the natord crate and inspired by natsort.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

natsort_rs-0.1.10a1-cp311-none-win_amd64.whl (168.9 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

natsort_rs-0.1.10a1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

natsort_rs-0.1.10a1-cp311-cp311-macosx_11_0_arm64.whl (235.8 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

natsort_rs-0.1.10a1-cp311-cp311-macosx_10_7_x86_64.whl (248.5 kB view hashes)

Uploaded CPython 3.11 macOS 10.7+ x86-64

natsort_rs-0.1.10a1-cp310-none-win_amd64.whl (168.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

natsort_rs-0.1.10a1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

natsort_rs-0.1.10a1-cp310-cp310-macosx_11_0_arm64.whl (235.8 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

natsort_rs-0.1.10a1-cp310-cp310-macosx_10_7_x86_64.whl (248.5 kB view hashes)

Uploaded CPython 3.10 macOS 10.7+ x86-64

natsort_rs-0.1.10a1-cp39-none-win_amd64.whl (168.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

natsort_rs-0.1.10a1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

natsort_rs-0.1.10a1-cp39-cp39-macosx_10_7_x86_64.whl (248.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.7+ x86-64

natsort_rs-0.1.10a1-cp38-none-win_amd64.whl (168.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

natsort_rs-0.1.10a1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (275.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

natsort_rs-0.1.10a1-cp38-cp38-macosx_10_7_x86_64.whl (248.4 kB view hashes)

Uploaded CPython 3.8 macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page