In: Computer Science
Given the following coordinates: (f, 9), (z, 2), (t, 4), (x, 8), (b, 1), (m, 7).
Sort the coordinates in Python (in ascending order) by comparing the first part of the
coordinates. You may use the sort() function in Python.
# sorting the list coordinates using first coordinate part
string or character
# for sorting strings bubble sorting is used
def SortFirstCoordinate(lst):
listlen = len(lst) #length for list
for i in range(0,listlen):
for j in range(0,
listlen-i-1):
if (lst[j][0]
> lst[j + 1][0]): #checking first coordinate is
greater than next coordinate if true then swap both
coordinates
tmpvar = lst[j]
lst[j]= lst[j + 1]
lst[j + 1]= tmpvar
return lst #returning sorted list
#main code starts
#list with coordinates
lst =[('f', 9), ('az', 2), ('t', 4),('x', 8), ('b', 1), ('m',
7)]
#displaying sorted list by first coordinate
print(SortFirstCoordinate(lst))