I've recently been using this code answered from another question:
foo = []
for line in test:
x = line.split()
y = int(x[1])
foo.append({"Name":x[0],"Average":str(y)})
print(x)
sorted_x = sorted(foo)
print sorted_x
I used this to sort averages. However, the code fails to sort averages producing results as such: [{'Average': '2.3333333333333335', 'Name': 'Alex'}]
[{'Average': '1.0', 'Name': 'Harry'}]
[{'Average': '9.0', 'Name': 'Lick'}]
As seen it's not sorted from highest to lowest or lowest to highest. How would I be able to change this code so it does sort from highest to lowest or vice verse?
Thanks
averagetostrif you need to sort byaverage.