3. natsort API

3.1. Standard API

3.1.1. natsorted()

3.1.2. The ns enum

3.1.3. natsort_key()

3.1.4. natsort_keygen()

3.1.5. os_sort_key()

3.1.6. os_sort_keygen()

3.2. Convenience Functions

3.2.1. os_sorted()

3.2.2. realsorted()

3.2.3. humansorted()

3.2.4. index_natsorted()

3.2.5. index_realsorted()

3.2.6. index_humansorted()

3.2.7. order_by_index()

3.2.8. Help With Bytes On Python 3

The official stance of natsort is to not support bytes for sorting; there is just too much that can go wrong when trying to automate conversion between bytes and str. But rather than completely give up on bytes, natsort provides three functions that make it easy to quickly decode bytes to str so that sorting is possible.

3.2.9. Help With Creating Function Keys

If you need to create a complicated key argument to (for example) natsorted() that is actually multiple functions called one after the other, the following function can help you easily perform this action. It is used internally to natsort, and has been exposed publicly for the convenience of the user.

If you need to be able to search your input for numbers using the same definition as natsort, you can do so using the following function. Given your chosen algorithm (selected using the ns enum), the corresponding regular expression to locate numbers will be returned.

3.2.10. Help With Type Hinting

If you need to explictly specify the types that natsort accepts or returns in your code, the following types have been exposed for your convenience.

Type

Purpose

natsort.NatsortKeyType

Returned by natsort.natsort_keygen(), and type of natsort.natsort_key

natsort.OSSortKeyType

Returned by natsort.os_sort_keygen(), and type of natsort.os_sort_key

natsort.KeyType

Type of key argument to natsort.natsorted() and natsort.natsort_keygen()

natsort.NatsortInType

The input type of natsort.NatsortKeyType

natsort.NatsortOutType

The output type of natsort.NatsortKeyType

natsort.NSType

The type of the ns enum