Skip to main content

Sort lists naturally

Project description

https://travis-ci.org/SethMMorton/natsort.svg?branch=master https://coveralls.io/repos/SethMMorton/natsort/badge.png?branch=master

Natural sorting for python.

Quick Description

When you try to sort a list of strings that contain numbers, the normal python sort algorithm sorts lexicographically, so you might not get the results that you expect:

>>> a = ['a2', 'a9', 'a1', 'a4', 'a10']
>>> sorted(a)
['a1', 'a10', 'a2', 'a4', 'a9']

Notice that it has the order (‘1’, ‘10’, ‘2’) - this is because the list is being sorted in lexicographical order, which sorts numbers like you would letters (i.e. ‘b’, ‘ba’, ‘c’).

natsort provides a function natsorted that helps sort lists “naturally”, either as real numbers (i.e. signed/unsigned floats or ints), or as versions. Using natsorted is simple:

>>> from natsort import natsorted
>>> a = ['a2', 'a9', 'a1', 'a4', 'a10']
>>> natsorted(a)
['a1', 'a2', 'a4', 'a9', 'a10']

natsorted identifies numbers anywhere in a string and sorts them naturally. Here are some other things you can do with natsort (please see the examples for a quick start guide, or the api for more details).

Sorting Versions

This is handled properly by default (as of natsort version >= 4.0.0):

>>> a = ['version-1.9', 'version-2.0', 'version-1.11', 'version-1.10']
>>> natsorted(a)
['version-1.9', 'version-1.10', 'version-1.11', 'version-2.0']

If you need to sort release candidates, please see http://pythonhosted.org//natsort/examples.html#rc-sorting for a useful hack.

Sorting by Real Numbers (i.e. Signed Floats)

This is useful in scientific data analysis and was the default behavior of natsorted for natsort version < 4.0.0. Use the realsorted function:

>>> from natsort import realsorted, ns
>>> a = ['num5.10', 'num-3', 'num5.3', 'num2']
>>> natsorted(a)
['num2', 'num5.3', 'num5.10', 'num-3']
>>> natsorted(a, alg=ns.REAL)
['num-3', 'num2', 'num5.10', 'num5.3']
>>> realsorted(a)  # shortcut for natsorted with alg=ns.REAL
['num-3', 'num2', 'num5.10', 'num5.3']

Locale-Aware Sorting (or “Human Sorting”)

This is where the non-numeric characters are ordered based on their meaning, not on their ordinal value, and a locale-dependent thousands separator is accounted for in the number. This can be achieved with the humansorted function:

>>> a = ['Apple', 'apple15', 'Banana', 'apple14,689', 'banana']
>>> natsorted(a)
['Apple', 'Banana', 'apple14,689', 'apple15', 'banana']
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> natsorted(a, alg=ns.LOCALE)
['apple15', 'apple14,689', 'Apple', 'banana', 'Banana']
>>> from natsort import humansorted
>>> humansorted(a)  # shortcut for natsorted with alg=ns.LOCALE
['apple15', 'apple14,689', 'Apple', 'banana', 'Banana']

You may find you need to explicitly set the locale to get this to work (as shown in the example). Please see http://pythonhosted.org/natsort/locale_issues.html and the Installation section below before using the humansorted function.

Sorting Mixed Types

You can mix and match int, float, and str (or unicode) types when you sort:

>>> a = ['4.5', 6, 2.0, '5', 'a']
>>> natsorted(a)
[2.0, '4.5', '5', 6, 'a']
>>> # On Python 2, sorted(a) would return [2.0, 6, '4.5', '5', 'a']
>>> # On Python 3, sorted(a) would raise an "unorderable types" TypeError

Handling Bytes on Python 3

natsort does not officially support the bytes type on Python 3, but convenience functions are provided that help you decode to str first:

>>> from natsort import as_utf8
>>> a = [b'a', 14.0, 'b']
>>> # On Python 2, natsorted(a) would would work as expected.
>>> # On Python 3, natsorted(a) would raise a TypeError (bytes() < str())
>>> natsorted(a, key=as_utf8) == [14.0, b'a', 'b']
True
>>> a = [b'a56', b'a5', b'a6', b'a40']
>>> # On Python 2, natsorted(a) would would work as expected.
>>> # On Python 3, natsorted(a) would return the same results as sorted(a)
>>> natsorted(a, key=as_utf8) == [b'a5', b'a6', b'a40', b'a56']
True

Generating a Reusable Sorting Key

All of the *sorted functions from the natsort are convenience functions around the something similar to the following:

>>> from natsort import natsort_keygen
>>> natsort_key = natsort_keygen()
>>> a = ['a2', 'a9', 'a1', 'a4', 'a10']
>>> natsorted(a) == sorted(a, key=natsort_key)
True

You can use this key for your own use (such as passing to list.sort). You can also customize the key with the ns enum (see the ns enum).

Other Useful Things

Shell script

natsort comes with a shell script called natsort, or can also be called from the command line with python -m natsort.

Requirements

natsort requires Python version 2.6 or greater or Python 3.3 or greater. It may run on (but is not tested against) Python 3.2.

Optional Dependencies

fastnumbers

The most efficient sorting can occur if you install the fastnumbers package (version >=0.7.1); it helps with the string to number conversions. natsort will still run (efficiently) without the package, but if you need to squeeze out that extra juice it is recommended you include this as a dependency. natsort will not require (or check) that fastnumbers is installed at installation.

PyICU

It is recommended that you install PyICU if you wish to sort in a locale-dependent manner, see http://pythonhosted.org/natsort/locale_issues.html for an explanation why.

Author

Seth M. Morton

History

These are the last three entries of the changelog. See the package documentation for the complete changelog.

05-08-2014 v. 5.0.0

  • ns.LOCALE/humansorted now accounts for thousands separators.

  • Refactored entire codebase to be more functional (as in use functions as units). Previously, the code was rather monolithic and difficult to follow. The goal is that with the code existing in smaller units, contributing will be easier.

  • Deprecated ns.TYPESAFE option as it is now always on (due to a new iterator-based algorithm, the typesafe function is now cheap).

  • Increased speed of execution (came for free with the new functional approach because the new factory function paradigm eliminates most if branches during execution).

    • For the most cases, the code is 30-40% faster than version 4.0.4.

    • If using ns.LOCALE or humansorted, the code is 1100% faster than version 4.0.4.

  • Improved clarity of documentaion with regards to locale-aware sorting.

  • Added a new chain_functions function for convenience in creating a complex user-given key from several existing functions.

11-01-2015 v. 4.0.4

  • Improved coverage of unit tests.

  • Unit tests use new and improved hypothesis library.

  • Fixed compatibility issues with Python 3.5

06-25-2015 v. 4.0.3

  • Fixed bad install on last release (sorry guys!).

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

natsort-5.0.0.zip (96.0 kB view details)

Uploaded Source

natsort-5.0.0.tar.gz (67.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

natsort-5.0.0-py2.py3-none-any.whl (32.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file natsort-5.0.0.zip.

File metadata

  • Download URL: natsort-5.0.0.zip
  • Upload date:
  • Size: 96.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for natsort-5.0.0.zip
Algorithm Hash digest
SHA256 b08513a4416b0f6e3bf9a5d659e41189bf3ceeec74de6e1e2b3ff4e23e89324b
MD5 664fc1c4af4cf2562326a5f1327f3a3d
BLAKE2b-256 60745c2e3acb0a2f548789719c4165ee69044547d38c6276e063c385e8d01696

See more details on using hashes here.

File details

Details for the file natsort-5.0.0.tar.gz.

File metadata

  • Download URL: natsort-5.0.0.tar.gz
  • Upload date:
  • Size: 67.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for natsort-5.0.0.tar.gz
Algorithm Hash digest
SHA256 b362a2bec8bc0dcdde0dd566703b48c2c59f607e897785c6c9253c96f4abb881
MD5 6662a32abb9fd79c899189ab3b29cf04
BLAKE2b-256 6316d418d0616b99583820929f2bd3ea06bdd67bf834ffb55e47a2c5af1b7bf6

See more details on using hashes here.

File details

Details for the file natsort-5.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for natsort-5.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 076546e61864d4a095cad453d301db5c8fdd1e5d340a3f92e3eef5caebdb7c8c
MD5 c4c518cf8d408a85d36d89347dfebe5a
BLAKE2b-256 98c89fdc4c9122bb0c7da63b28f165d323188597e795d2d52f884c5e0a33bec0

See more details on using hashes here.

Supported by

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