Sort Tuples by First and Second Elements

  • Share this:

Code introduction


This function is used to sort a list of tuples based on the first element of each tuple, and if the first elements are the same, it will sort based on the second element.


Technology Stack : Sorting

Code Type : Function

Code Difficulty : Intermediate


                
                    
def aord(arg1, arg2):
    """
    Sort a list of tuples based on the first element and then on the second element.
    """
    return sorted(arg1, key=lambda x: (x[0], x[1]))                
              
Tags: