Skip to main content

Sort lists naturally

Project description

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

Natural sorting for python. Check out the source code at https://github.com/SethMMorton/natsort.

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 real numbers anywhere in a string and sorts them naturally.

Sorting version numbers is just as easy with the versorted function:

>>> from natsort import versorted
>>> a = ['version-1.9', 'version-2.0', 'version-1.11', 'version-1.10']
>>> versorted(a)
['version-1.9', 'version-1.10', 'version-1.11', 'version-2.0']
>>> natsorted(a)  # natsorted tries to sort as signed floats, so it won't work
['version-2.0', 'version-1.9', 'version-1.11', 'version-1.10']

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

The natsort algorithm does other fancy things like

  • recursively descend into lists of lists

  • sort file paths correctly

  • allow custom sorting keys

  • exposes a natsort_key generator to pass to list.sort

Please see the package documentation for more details, including additional examples and recipes.

Shell script

natsort comes with a shell script called natsort, or can also be called from the command line with python -m natsort. The command line script is only installed onto your PATH if you don’t install via a wheel. There is apparently a known bug with the wheel installation process that will not create entry points.

Requirements

natsort requires python version 2.6 or greater (this includes python 3.x). To run version 2.6, 3.0, or 3.1 the argparse module is required.

Depreciation Notices

  • In natsort version 4.0.0, the natsort_key function will be removed from the public API. All future development should use natsort_keygen in preparation for this.

  • In natsort version 3.1.0, the shell script changed how it interpreted input; previously, all input was assumed to be a filepath, but as of 3.1.0 input is just treated as a string. For most cases the results are the same.

    • As of natsort version 3.4.0, a --path option has been added to force the shell script to interpret the input as filepaths.

Author

Seth M. Morton

History

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

07-19-2014 v. 3.4.0

  • Fixed a bug that caused user’s options to the ‘natsort_key’ to not be passed on to recursive calls of ‘natsort_key’.

  • Added a ‘natsort_keygen’ function that will generate a wrapped version of ‘natsort_key’ that is easier to call. ‘natsort_key’ is now set to depreciate at natsort version 4.0.0.

  • Added an ‘as_path’ option to ‘natsorted’ & co. that will try to treat input strings as filepaths. This will help yield correct results for OS-generated inputs like ['/p/q/o.x', '/p/q (1)/o.x', '/p/q (10)/o.x', '/p/q/o (1).x'].

  • Massive performance enhancements for string input (1.8x-2.0x), at the expense of reduction in speed for numeric input (~2.0x).

    • This is a good compromise because the most common input will be strings, not numbers, and sorting numbers still only takes 0.6x the time of sorting strings. If you are sorting only numbers, you would use ‘sorted’ anyway.

  • Added the ‘order_by_index’ function to help in using the output of ‘index_natsorted’ and ‘index_versorted’.

  • Added the ‘reverse’ option to ‘natsorted’ & co. to make it’s API more similar to the builtin ‘sorted’.

  • Added more unit tests.

  • Added auxiliary test code that helps in profiling and stress-testing.

  • Reworked the documentation, moving most of it to PyPI’s hosting platform.

  • Added support for coveralls.io.

  • Entire codebase is now PyFlakes and PEP8 compliant.

06-28-2014 v. 3.3.0

  • Added a ‘versorted’ method for more convenient sorting of versions.

  • Updated command-line tool –number_type option with ‘version’ and ‘ver’ to make it more clear how to sort version numbers.

  • Moved unit-testing mechanism from being docstring-based to actual unit tests in actual functions.

    • This has provided the ability determine the coverage of the unit tests (99%).

    • This also makes the pydoc documentation a bit more clear.

  • Made docstrings for public functions mirror the README API.

  • Connected natsort development to Travis-CI to help ensure quality releases.

06-20-2014 v. 3.2.1

  • Re-“Fixed” unorderable types issue on Python 3.x - this workaround is for when the problem occurs in the middle of the string.

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-3.4.0.zip (24.5 kB view details)

Uploaded Source

natsort-3.4.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

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

natsort-3.4.0-py2.py3-none-any.whl (18.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file natsort-3.4.0.zip.

File metadata

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

File hashes

Hashes for natsort-3.4.0.zip
Algorithm Hash digest
SHA256 54c60ff8b52f9788c792108fbce480a24ad8aa59310d254b8aacc78f69183d48
MD5 f69b24088230d1362c2e549669222dd9
BLAKE2b-256 debcc4ec03eee23a7274f7c27626e09ef023381596dd55fa162757bccf3b169b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for natsort-3.4.0.tar.gz
Algorithm Hash digest
SHA256 a58ea2410eedf587adda3f2a4ef93a820a99028b253b510c79ef5b4a11cd1dbb
MD5 ee053a5b98f24f1c25106872d498d526
BLAKE2b-256 03b1f2b07ed54552704ca08ea312a5ee84a50b5065e5d9a8c0040ed06a37b502

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for natsort-3.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 95bff395173ae6f4abb292a694a1e646f994bb97bed94aa4a53d54c44c308148
MD5 d9c626f851ae6d8f21e4412c50492f75
BLAKE2b-256 3f45e96e6d90c7e012678f15ea9bffc99c506129fe648aab4d20c043c39f43a3

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