I'm a beginner in Python, I was wondering how do I sort specific data in a file.
For example, if I want to sort and display a record of Coaches Hourly Pay Rate in ascending order.
I only managed to sort the coach name and was unable to sort other categories in the file.
The ideal output will be like the text file but in ascending order of the category I sort.
Coach.txt
Coach name:Adam Coach ID:001 Sport:Swimming Hourly Wages:RM100 Coach Overall Performance:4,
Coach name:Jennie Coach ID:002 Sport:Badminton Hourly Wages:RM200 Coach Overall Performance:5,
Coach name:Roger Coach ID:003 Sport:Football Hourly Wages:RM150 Coach Overall Performance:4,
Coach name:Artemis Coach ID:004 Sport:Archery Hourly Wages:RM300 Coach Overall Performance:5,
Coach name:Rose Coach ID:005 Sport:Gymnastics Hourly Wages:RM300 Coach Overall Performance:5,
Coach name:Brandon Coach ID:006 Sport:Volleyball Hourly Wages:RM100 Coach Overall Performance:4,
Coach name:Kobe Coach ID:007 Sport:Basketball Hourly Wages:RM500 Coach Overall Performance:5,
Coach name:Muthu Coach ID:008 Sport:Cricket Hourly Wages:RM100 Coach Overall Performance:3,
Coach name:Chong Coach ID:009 Sport:Tennis Hourly Wages:Rm150 Coach Overall Performance:4,
Coach name:Armin Coach ID:010 Sport:Tabble Tennis Hourly Wages:RM150 Coach Overall Performance:5,
f = open("coach.txt", "r")
payrate = []
i = 0
for line in f:
split = line.split(",")
payrate.append(split[0].rstrip())
for i in range(0, len(payrate)):
for j in range(i+1, len(payrate)):
if (payrate[i]) > (payrate[j]):
temp = payrate[i]
payrate[i] = payrate[j]
payrate[j] = temp
print("The coach records by ascending order of name:\n", payrate)
f.close()