10 examples of 'python sort list alphabetically' in Python

Every line of 'python sort list alphabetically' 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
909""" Contains(self: Queue[T], item: T) -> bool """
910pass
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
1def gnome_sort(a):
2 i, j, size = 1, 2, len(a)
3 while i < size:
4 if a[i-1] <= a[i]:
5 i, j = j, j+1
6 else:
7 a[i-1], a[i] = a[i], a[i-1]
8 i -= 1
9 if i == 0:
10 i, j = j, j+1
11 return a
59def natural_sort(ls):
60 convert = lambda text: int(text) if text.isdigit() else text.lower()
61 alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
62 return sorted(ls, key = alphanum_key)
22def Quick_Sort(list):
23 Quick(list, 0, len(list) - 1)
38def sort(a):
39 mergeSort(a,0,len(a)-1)
40def test_simple_sort(self):
41 input_lines = [
42 '1',
43 '3',
44 '2',
45 ]
46 expected_output_lines = [
47 '1',
48 '2',
49 '3',
50 ]
51 self.assertEqual(sort_lines(input_lines), expected_output_lines)
40def testLtSort(self):
41 self.run_test(lt_sort, self.source)
15def sort_by_last_letter_lambda(arr):
16 return sorted(arr, key=lambda s: s[-1])
40def natsort(original: Iterable[str]) -> List[str]:
41 """
42 Sort a list in natural order.
43
44 .. code-block:: python
45
46 >>> sorted(map(str, range(12)))
47 ['0', '1', '10', '11', '2', '3', '4', '5', '6', '7', '8', '9']
48 >>> natsort(map(str, range(12)))
49 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']
50
51 :param original: The original iterable.
52
53 :return: The sorted list.
54
55 .. seealso::
56
57 https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
58 """
59 return sorted(original, key=alnum_key)

Related snippets