How to use 'python sorted' in Python

Every line of 'python sorted' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
70def sorted(lst, cmp=None, key=None, reverse=None):
71 "sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list"
72 sorted_lst = list(lst)
73 sorted_lst.sort(cmp, key, reverse)
74 return sorted_lst
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

314def realsorted(seq, key=None, reverse=False, alg=ns.DEFAULT):
315 """
316 Convenience function to properly sort signed floats.
317
318 A signed float in a string could be "a-5.7". This is a wrapper around
319 ``natsorted(seq, alg=ns.REAL)``.
320
321 The behavior of :func:`realsorted` for `natsort` version >= 4.0.0
322 was the default behavior of :func:`natsorted` for `natsort`
323 version < 4.0.0.
324
325 Parameters
326 ----------
327 seq : iterable
328 The input to sort.
329
330 key : callable, optional
331 A key used to determine how to sort each element of the sequence.
332 It is **not** applied recursively.
333 It should accept a single argument and return a single value.
334
335 reverse : {{True, False}}, optional
336 Return the list in reversed sorted order. The default is
337 `False`.
338
339 alg : ns enum, optional
340 This option is used to control which algorithm `natsort`
341 uses when sorting. For details into these options, please see
342 the :class:`ns` class documentation. The default is `ns.REAL`.
343
344 Returns
345 -------
346 out : list
347 The sorted input.
348
349 See Also
350 --------
351 index_realsorted : Returns the sorted indexes from `realsorted`.
352
353 Examples
354 --------
355 Use `realsorted` just like the builtin `sorted`::
356
357 >>> a = ['num5.10', 'num-3', 'num5.3', 'num2']
358 >>> natsorted(a)
359 ['num2', 'num5.3', 'num5.10', 'num-3']
360 >>> realsorted(a)
361 ['num-3', 'num2', 'num5.10', 'num5.3']
362
363 """
364 return natsorted(seq, key, reverse, alg | ns.REAL)

Related snippets