4.5. realsorted()

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

Convenience function to properly sort signed floats.

Convenience function to properly sort signed floats within strings (i.e. “a-5.7”). This is a wrapper around natsorted(seq, alg=ns.REAL).

The behavior of realsorted() for natsort version >= 4.0.0 was the default behavior of natsorted() for natsort version < 4.0.0.

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.REAL.
Returns:

out – The sorted sequence.

Return type:

list

See also

index_realsorted()
Returns the sorted indexes from realsorted.

Examples

Use realsorted just like the builtin sorted:

>>> a = ['num5.10', 'num-3', 'num5.3', 'num2']
>>> natsorted(a)
['num2', 'num5.3', 'num5.10', 'num-3']
>>> realsorted(a)
['num-3', 'num2', 'num5.10', 'num5.3']