10 examples of 'python sort one list by another' in Python

Every line of 'python sort one list by another' 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
909""" Contains(self: Queue[T], item: T) -> bool """
910pass
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

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
15def SortTwoLists(primary, secondary):
16 # sort two lists by order of elements of the primary list
17 paired_sorted = sorted(zip(primary, secondary), key=lambda x: x[0])
18 return (map(list, zip(*paired_sorted))) # two lists
22def Quick_Sort(list):
23 Quick(list, 0, len(list) - 1)
38def sort(a):
39 mergeSort(a,0,len(a)-1)
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
5def bubblesort(list):
6 swapped = True
7 while swapped:
8 print
9 print "New iteration..."
10 swapped = False
11 for i in range(len(list)-1):
12 if(list[i] > list[i+1]):
13 print "Index: " + str(i) + " - Swap " + str(list[i]) + " with " + str(list[i+1])
14 tmp = list [i]
15 list[i] = list[i+1]
16 list[i+1] = tmp
17 swapped = True
18 print list
19 print "Nothing left to swap. Done"
20 return list
21def mergeSort(alist):
22 if len(alist)>1:
23 mid = len(alist)//2
24 lefthalf = alist[:mid]
25 righthalf = alist[mid:]
26 lefthalflength = len(lefthalf)
27 righthalflength = len(righthalf)
28 mergeSort(lefthalf)
29 mergeSort(righthalf)
30 i=0
31 j=0
32 k=0
33 while i < lefthalflength and j < righthalflength:
34 #IMPORTANT: < works but <= makes merge sort stable!
35 if lefthalf[i] <= righthalf[j]:
36 alist[k]=lefthalf[i]
37 i=i+1
38 else:
39 alist[k]=righthalf[j]
40 j=j+1
41 k=k+1
42
43 while i < lefthalflength:
44 alist[k]=lefthalf[i]
45 i=i+1
46 k=k+1
47
48 while j < righthalflength:
49 alist[k]=righthalf[j]
50 j=j+1
51 k=k+1
13def selectionSort2(list):
14 for index in range(0,len(list)-1):
15 indexMin = 0
16 for i in range(index+1,len(list)):
17 if list[i] < list[indexMin]: indexMin = i
18 if indexMin > index:
19 temp = list[index]
20 list[index] = list[indexMin]
21 list[indexMin] = temp
10def unordered_list_cmp(list1, list2):
11 # Check lengths first for slight improvement in performance
12 return len(list1) == len(list2) and sorted(list1) == sorted(list2)

Related snippets