summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2014-03-01 12:25:46 -0800
committerSeth M Morton <seth.m.morton@gmail.com>2014-03-01 12:25:46 -0800
commit956572d68e75f8428554ec28714ae0ee127d5cc0 (patch)
treebc0aa38eaa21c9ed6fe717980b5bda5e0459f176
parent626ceed146a658c7b588b922cbca8fc266555274 (diff)
parent508f2af9d86d36977e888ac97ebe6b65195fd590 (diff)
downloadnatsort-3.1.1.tar.gz
Merge branch 'release/3.1.1'3.1.1
-rw-r--r--README.rst14
-rw-r--r--natsort/__init__.py1
-rw-r--r--natsort/__main__.py2
-rw-r--r--natsort/_version.py3
-rw-r--r--natsort/natsort.py28
-rw-r--r--natsort/py23compat.py1
6 files changed, 46 insertions, 3 deletions
diff --git a/README.rst b/README.rst
index 24234d9..8f78580 100644
--- a/README.rst
+++ b/README.rst
@@ -54,6 +54,14 @@ when you sort::
>>> # On Python 2, sorted(a) would return [2.3, 6, '4.5', '5']
>>> # On Python 3, sorted(a) would raise an "unorderable types" TypeError
+The natsort algorithm will recursively descend into lists of lists so you can sort by
+the sublist contents::
+
+ >>> data = [['a1', 'a5'], ['a1', 'a40'], ['a10', 'a1'], ['a2', 'a5']]
+ >>> sorted(data)
+ [['a1', 'a40'], ['a1', 'a5'], ['a10', 'a1'], ['a2', 'a5']]
+ >>> natsorted(data)
+ [['a1', 'a5'], ['a1', 'a40'], ['a2', 'a5'], ['a10', 'a1']]
The Sorting Algorithms
''''''''''''''''''''''
@@ -326,6 +334,12 @@ Seth M. Morton
History
-------
+03-01-2014 v. 3.1.1
+'''''''''''''''''''
+
+ - Added ability to sort lists of lists
+ - Cleaned up import statements
+
01-20-2014 v. 3.1.0
'''''''''''''''''''
diff --git a/natsort/__init__.py b/natsort/__init__.py
index 47e2ceb..0d10cf2 100644
--- a/natsort/__init__.py
+++ b/natsort/__init__.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
+
from .natsort import natsort_key, natsorted, index_natsorted
from ._version import __version__
diff --git a/natsort/__main__.py b/natsort/__main__.py
index e3242c6..f4c3efe 100644
--- a/natsort/__main__.py
+++ b/natsort/__main__.py
@@ -1,8 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import print_function, division, unicode_literals
+
import sys
import os
import re
+
from .natsort import natsort_key, natsorted, int_nosign_re, int_sign_re
from .natsort import float_sign_exp_re, float_nosign_exp_re
from .natsort import float_sign_noexp_re, float_nosign_noexp_re
diff --git a/natsort/_version.py b/natsort/_version.py
index d763f0a..fdab7b2 100644
--- a/natsort/_version.py
+++ b/natsort/_version.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
-__version__ = '3.1.0'
+
+__version__ = '3.1.1'
diff --git a/natsort/natsort.py b/natsort/natsort.py
index ef494c5..90d197c 100644
--- a/natsort/natsort.py
+++ b/natsort/natsort.py
@@ -64,13 +64,23 @@ You can mix types with natsorted. This can get around the new
>>> natsorted(a)
[{u}'2.5', 4.5, 6, {u}'7']
+Natsort will recursively descend into lists of lists so you can sort by the sublist contents.
+
+ >>> data = [['a1', 'a5'], ['a1', 'a40'], ['a10', 'a1'], ['a2', 'a5']]
+ >>> sorted(data)
+ [[{u}'a1', {u}'a40'], [{u}'a1', {u}'a5'], [{u}'a10', {u}'a1'], [{u}'a2', {u}'a5']]
+ >>> natsorted(data)
+ [[{u}'a1', {u}'a5'], [{u}'a1', {u}'a40'], [{u}'a2', {u}'a5'], [{u}'a10', {u}'a1']]
+
"""
from __future__ import unicode_literals
-from .py23compat import u_format, py23_basestring, py23_range, py23_str, py23_zip
+
import re
import sys
+from .py23compat import u_format, py23_basestring, py23_range, py23_str, py23_zip
+
__doc__ = u_format(__doc__) # Make sure the doctest works for either python2 or python3
# The regex that locates floats
@@ -97,6 +107,7 @@ regex_and_num_function_chooser = {
(None, False, False) : (int_nosign_re, int),
}
+
@u_format
def remove_empty(s):
"""\
@@ -181,11 +192,24 @@ def natsort_key(s, number_type=float, signed=True, exp=True):
>>> natsort_key('a-5.034e1', number_type=None) == natsort_key('a-5.034e1', number_type=int, signed=False)
True
+ Iterables are parsed recursively so you can sort lists of lists.
+
+ >>> natsort_key(('a1', 'a10'))
+ (({u}'a', 1.0), ({u}'a', 10.0))
+
+ You can give numbers, too.
+
+ >>> natsort_key(10)
+ (10,)
+
"""
# If we are dealing with non-strings, return now
if not isinstance(s, py23_basestring):
- return (s,)
+ if hasattr(s, '__getitem__'):
+ return tuple(natsort_key(x) for x in s)
+ else:
+ return (s,)
# Convert to the proper tuple and return
inp_options = (number_type, signed, exp)
diff --git a/natsort/py23compat.py b/natsort/py23compat.py
index f5af384..1b84bb2 100644
--- a/natsort/py23compat.py
+++ b/natsort/py23compat.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
+
import functools
import sys