4.2. natsorted()
¶
-
natsort.
natsorted
(seq, key=None, reverse=False, alg=0, **_kwargs)¶ Sorts an iterable naturally.
Sorts an iterable naturally (alphabetically and numerically), not lexicographically. Returns a list containing a sorted copy of the iterable.
Parameters: - seq (iterable) – The iterable to sort.
- key (callable, optional) – A key used to determine how to sort each element of the iterable. It is not applied recursively. It should accept a single argument and return a single value.
- reverse ({True, False}, optional) – Return the list in reversed sorted order. The default is False.
- 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.INT.
Returns: out – The sorted sequence.
Return type: See also
natsort_keygen()
- Generates the key that makes natural sorting possible.
realsorted()
- A wrapper for
natsorted(seq, alg=ns.REAL)
. 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) ['num2', 'num3', 'num5']