# 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
``909""" Contains(self: Queue[T], item: T) -&gt; bool """910pass``
``1def gnome_sort(a):2    i, j, size = 1, 2, len(a)3    while i &lt; size:4        if a[i-1] &lt;= a[i]:5            i, j = j, j+16        else:7            a[i-1], a[i] = a[i], a[i-1]8            i -= 19            if i == 0:10                i, j = j, j+111    return a``
``15def SortTwoLists(primary, secondary):16    # sort two lists by order of elements of the primary list17    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) --&gt; new sorted list"72    sorted_lst = list(lst)73    sorted_lst.sort(cmp, key, reverse)74    return sorted_lst``
``5def bubblesort(list):6        swapped = True7        while swapped:8                print9                print "New iteration..."10                swapped = False11                for i in range(len(list)-1):12                        if(list[i] &gt; 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] = tmp17                                swapped = True18                                print list19        print "Nothing left to swap. Done"20        return list``
``21def mergeSort(alist):22    if len(alist)&gt;1:23        mid = len(alist)//224        lefthalf = alist[:mid]25        righthalf = alist[mid:]26        lefthalflength = len(lefthalf)27        righthalflength = len(righthalf)28        mergeSort(lefthalf)29        mergeSort(righthalf)30        i=031        j=032        k=033        while i &lt; lefthalflength and j &lt; righthalflength:34	    #IMPORTANT: &lt; works but &lt;= makes merge sort stable! 35            if lefthalf[i] &lt;= righthalf[j]:   36                alist[k]=lefthalf[i]37                i=i+138            else:39                alist[k]=righthalf[j]40                j=j+141            k=k+14243        while i &lt; lefthalflength:44            alist[k]=lefthalf[i]45            i=i+146            k=k+14748        while j &lt; righthalflength:49            alist[k]=righthalf[j]50            j=j+151            k=k+1``
``13def selectionSort2(list):14    for index in range(0,len(list)-1):15        indexMin = 016        for i in range(index+1,len(list)):17            if list[i] &lt; list[indexMin]: indexMin = i18        if indexMin &gt; 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 performance12    return len(list1) == len(list2) and sorted(list1) == sorted(list2)``