Sometimes, while working with data, we can have a problem of sorting them. There are many types of basis on which sorting can be performed. But this article discusses sorting on basis of difference of both elements of pair. Let’s discuss certain ways in which this can be done.

**Method #1 : Using sorted() + lambda**

The combination of above functionalities can be used to solve this problem. In this, sorting is performed by

`sorted()`

and lambda function is fed as key to perform desired sorting.
`# Python3 code to demonstrate working of ` `# Sort tuple list on basis of difference of elements ` `# using sorted() + lambda ` ` ` `# initialize list ` `test_list ` `=` `[(` `1` `, ` `4` `), (` `6` `, ` `5` `), (` `8` `, ` `10` `)] ` ` ` `# printing original list ` `print` `(` `"The original list : "` `+` `str` `(test_list)) ` ` ` `# Sort tuple list on basis of difference of elements ` `# using sorted() + lambda ` `res ` `=` `sorted` `(test_list, key ` `=` `lambda` `sub: ` `abs` `(sub[` `1` `] ` `-` `sub[` `0` `])) ` ` ` `# printing result ` `print` `(` `"List after sorting by difference : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list : [(1, 4), (6, 5), (8, 10)] List after sorting by difference : [(6, 5), (8, 10), (1, 4)]

**Method #2 : Using sort() + comparator + cmp()**

This is yet another way to perform this task. In this, we pass the

`sort()`

, a comparator function which performs the similar difference logic using `cmp()`

. Works only in Python2.
`# Python code to demonstrate working of ` `# Sort tuple list on basis of difference of elements ` `# using sort() + comparator + cmp() ` ` ` `# comparator function ` `def` `diff_sort(ele1, ele2): ` ` ` `return` `cmp` `(ele2[` `0` `] ` `-` `ele2[` `1` `], ele1[` `0` `] ` `-` `ele1[` `1` `]) ` ` ` `# initialize list ` `test_list ` `=` `[(` `1` `, ` `4` `), (` `6` `, ` `5` `), (` `8` `, ` `10` `)] ` ` ` `# printing original list ` `print` `(` `"The original list : "` `+` `str` `(test_list)) ` ` ` `# Sort tuple list on basis of difference of elements ` `# using sort() + comparator + cmp() ` `test_list.sort(diff_sort) ` ` ` `# printing result ` `print` `(` `"List after sorting by difference : "` `+` `str` `(test_list)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list : [(1, 4), (6, 5), (8, 10)] List after sorting by difference : [(6, 5), (8, 10), (1, 4)]

## Recommended Posts:

- Python - Sort List items on basis of their Digits
- Python | Sort tuple list by Nth element of tuple
- Python - Sort on basis of reverse Strings
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Raise elements of tuple as power to another tuple
- Python | Sorting URL on basis of Top Level Domain
- Python | Replace tuple according to Nth tuple element
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | sort list of tuple based on sum
- Python - Sort by Frequency of second element in Tuple List
- Python - Group first elements by second elements in Tuple list
- Python | Pair and combine nested list to tuple list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Convert mixed data types tuple list to string list
- Python | Convert List of Dictionary to Tuple list

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.