# Python | Inverse Sorting String

• Last Updated : 11 May, 2020

Sometimes, while participating in a competitive programming test, we can be encountered with a problem in which we require to sort a pair in opposite orders by indices. This particular article focuses on solving a problem in which we require to sort the number in descending order and then the String in increasing order. This is the type of problem which is common in the sorting of pairs. Let’s discuss a way in which this can be solved.

Method : Using `sorted()` + lambda
The combination of these functions can be used to perform this task. In these, we pass to lambda function the negative of the values so that the increasing order of number is evaluated as decreasing and hence a successful hack to perform this task.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

 `# Python3 code to demonstrate working of``# Inverse sorting String, Integer tuple list``# Using sorted() + lambda`` ` `# initializing list``test_list ``=` `[(``"Geeks"``, ``5``), (``"For"``, ``3``), (``"Geeks"``, ``6``), (``"Is"``, ``5``), ``             ``(``"Best"``, ``7` `), (``"For"``, ``5``), (``"CS"``, ``3``)]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# Inverse sorting String, Integer tuple list``# Using sorted() + lambda``res ``=` `sorted``(test_list, key ``=` `lambda` `sub: (``-``sub[``1``], sub[``0``]))`` ` `# printing result ``print``(``"The list after inverse sorted tuple elements : "` `+` `str``(res))`
Output :

The original list is : [(‘Geeks’, 5), (‘For’, 3), (‘Geeks’, 6), (‘Is’, 5), (‘Best’, 7), (‘For’, 5), (‘CS’, 3)]
The list after inverse sorted tuple elements : [(‘Best’, 7), (‘Geeks’, 6), (‘For’, 5), (‘Geeks’, 5), (‘Is’, 5), (‘CS’, 3), (‘For’, 3)]

My Personal Notes arrow_drop_up