3.3. natsorted()

natsort.natsorted(seq, key=None, number_type=<type 'float'>, signed=None, exp=None, reverse=False, as_path=None, alg=0)

Sorts a sequence naturally.

Sorts a sequence naturally (alphabetically and numerically), not lexicographically. Returns a new copy of the sorted sequence as a list.

Parameters:

seq : iterable

The sequence to sort.

key : callable, optional

A key used to determine how to sort each element of the sequence. It is not applied recursively. It should accept a single argument and return a single value.

number_type : {None, float, int}, optional

Depreciated as of version 3.5.0 and will become an undocumented keyword-only argument in 4.0.0. Please use the alg argument for all future development. See ns class documentation for details.

signed : {True, False}, optional

Depreciated as of version 3.5.0 and will become an undocumented keyword-only argument in 4.0.0. Please use the alg argument for all future development. See ns class documentation for details.

exp : {True, False}, optional

Depreciated as of version 3.5.0 and will become an undocumented keyword-only argument in 4.0.0. Please use the alg argument for all future development. See ns class documentation for details.

reverse : {True, False}, optional

Return the list in reversed sorted order. The default is False.

as_path : {True, False}, optional

Depreciated as of version 3.5.0 and will become an undocumented keyword-only argument in 4.0.0. Please use the alg argument for all future development. See ns class documentation for details.

alg : ns enum, optional

This option is used to control which algorithm natsort uses when sorting. For details into these options, please see the ns class documentation. The default is ns.FLOAT.

Returns:

out: list

The sorted sequence.

See also

natsort_keygen
Generates the key that makes natural sorting possible.
versorted
A wrapper for natsorted(seq, alg=ns.VERSION).
humansorted
A wrapper for natsorted(seq, alg=ns.LOCALE).
index_natsorted
Returns the sorted indexes from natsorted.

Examples

Use natsorted just like the builtin sorted:

>>> a = ['num3', 'num5', 'num2']
>>> natsorted(a)
[u'num2', u'num3', u'num5']