I have a text file(data.txt) containing names and scores 1:1 i.e.:
Mike = 1\n John = 2\n Cam = 3\n
I want to sort the integers along with the corresponding name in ascending and descending order.
with open(filepath, 'r') as file:
list = []
for line in file:
list.append(line[1:-1].split(","))
list.sort(key=lambda x: int(x[4]))
Yes, i have done some research however it doesn't work, i was hoping one of you guys could help me fix the code above. I know i must convert the data within the text file into a list then sort the list then put write back to the text file, but i am not sure how. Source: How do i sort a text file numerically highest to lowest?