'Sort array output python

array1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]

I'm trying to write a code to sort the array



Solution 1:[1]

Not clear what "sort the array in the format of another array" meams? If you mean sort a subset of the aray so that the order in the subset is the same as the order in the array, you can use something like

sorted(array2,key = lambda k:array1.index(k))

which will sort array2 so that any two elements of array2 are in the same order as in array 1. Note "subset" is needed. This will fail if there is an element of array2 that is not in array1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 William