I wish to sort a list by the first value in each tuple, however my code does not produce any real sorted list, although the order of the list does change. My list contains tuples with a value for the 'PlayerCode' which is a unique key for each player. And 'Average Points' which is the average amount of points that the player scores each game. I wish to sort this list by 'Average Points' although I cant seem to get it right. Here is my code
def getKey(item):
return item[0]
def sortByPoints():
foundPlayers = []
with open ('PlayerList.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
foundPlayers.append((row['Average PTS'], row['PlayerCode']))
print(foundPlayers)
sortedPlayers = sorted(foundPlayers, key = getKey)
print (sortedPlayers)
Like I said, this does change the order of the list, but not the way im looking for, nor can I see any real pattern to which it has changed it to. I am using python 3.4
Here is the output of the two lists
unsorted: [('7', '1'), ('8', '2'), ('4', '3'), ('28', '4'), ('0', '5'), ('18', '6'), ('10', '7'), ('9', '8'), ('10', '9'), ('2', '10'), ('4', '11'), ('2', '12'), ('3', '13'), ('4', '14'), ('2', '15')]
sorted: [('0', '5'), ('10', '7'), ('10', '9'), ('18', '6'), ('2', '10'), ('2', '12'), ('2', '15'), ('28', '4'), ('3', '13'), ('4', '3'), ('4', '11'), ('4', '14'), ('7', '1'), ('8', '2'), ('9', '8')]