4.4. humansorted()

natsort.humansorted(seq, key=None, reverse=False, alg=0)

Convenience function to properly sort non-numeric characters.

Convenience function to properly sort non-numeric characters in a locale-aware fashion (a.k.a “human sorting”). This is a wrapper around natsorted(seq, alg=ns.LOCALE).

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.
  • 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.LOCALE.
Returns:

out – The sorted sequence.

Return type:

list

See also

index_humansorted()
Returns the sorted indexes from humansorted.

Notes

Please read Possible Issues with humansorted() or ns.LOCALE before using humansorted.

Examples

Use humansorted just like the builtin sorted:

>>> a = ['Apple', 'Banana', 'apple', 'banana']
>>> natsorted(a)
['Apple', 'Banana', 'apple', 'banana']
>>> humansorted(a)
['apple', 'Apple', 'banana', 'Banana']