This is a Python Program to sort the list according to the second element in the sublist.
Problem Description
The program takes a list of lists and sorts the list according to the second element in the sublist.
Problem Solution
1. Take in a list containing sublists.
2. Using two for loops, use bubble sort to sort the sublists based on the second value of the sublist.
3. If the second element of the first sublist is greater than the second element of the second sublist, exchange the entire sublist.
4. Print the sorted list.
5. Exit.
Program/Source Code
Here is source code of the Python Program to sort the list according to the second element in the sublist. The program output is also shown below.
a=[[‘A’,34],[‘B’,21],[‘C’,26]]
for i in range(0,len(a)):
for j in range(0,len(a)-i-1):
if(a[j][1]>a[j+1][1]):
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
print(a)
Runtime Test Cases
Case 1:
[[‘B’, 21], [‘C’, 26], [‘A’, 34]]