Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

natsort-rs

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

natsort-rs

A blazing fast natural sorting library for Python

  • 0.1.14
  • PyPI
  • Socket score

Maintainers
1

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]

Benchmark

No. of itemsDuration natsort [s]Duration natsort-rs [s]Relative speedup
100.000060.0000016.8
1000.000940.0000244.3
10000.002810.0002212.7
100000.028350.0026210.8
1000000.297120.033348.9
10000003.312070.453337.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

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc