-1

So I found how to sort a list of tuples in descending order, but I'm trying to figure out how to do sorting in ascending order, could somebody please help me with this (I'm stuck for quite a bit XD). Here is my code:

def Sort_Tuple(tup):
    tup.sort(key=lambda x: x[1])
    return tup

tup = [('A', 10), ('B', 5), ('C', 20), ('D', 15)]
print(Sort_Tuple(tup))
#RETURNS [('B', 5), ('A', 10), ('D', 15), ('C', 20)]
#I want [('C', 20), ('D', 15), ('A', 10), ('B', 5)]
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
TheCreator
  • 155
  • 6

1 Answers1

0

Add reverse=True keyword argument

def Sort_Tuple(tup):
    tup.sort(key=lambda x: x[1],reverse=True)
    return tup
tup = [('A', 10), ('B', 5), ('C', 20), ('D', 15)]
print(Sort_Tuple(tup))
Wasif
  • 14,755
  • 3
  • 14
  • 34