Skip to content

Commit c77c8d0

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#296 from Nostalagic-surbhi/patch-1
Added a program of sorting
2 parents ca76487 + 85d70e6 commit c77c8d0

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

sort.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Subscribed by code house
2+
# Python program to demonstrate sorting by user's
3+
# choice
4+
5+
6+
# function to return the second element of the
7+
# two elements passed as the parameter
8+
9+
def sortSecond(val):
10+
11+
return val[1]
12+
13+
14+
# list1 to demonstrate the use of sorting
15+
# using using second key
16+
17+
list1 = [(1, 2), (3, 3), (1, 1)]
18+
19+
20+
# sorts the array in ascending according to
21+
# second element
22+
23+
list1.sort(key = sortSecond)
24+
25+
print(list1)
26+
27+
28+
# sorts the array in descending according to
29+
# second element
30+
31+
list1.sort(key = sortSecond, reverse = True)
32+
33+
print(list1

0 commit comments

Comments
 (0)