Sorting List by Key List in Python

  • Share this:

Code introduction


This function accepts two arguments, the first argument is the list to be sorted, and the second argument is the key list, which specifies the sorting basis for each element. The function uses Python's built-in sorted function for sorting.


Technology Stack : Python built-in library

Code Type : Sort function

Code Difficulty : Intermediate


                
                    
def aord(arg1, arg2):
    try:
        return sorted(arg1, key=lambda x: arg2[x])
    except TypeError:
        return "Error: 'arg1' and 'arg2' must be of the same type."