For an assignment I have to create my own sorting algorithm to sort a list containing football scores.
The information is formatted as follows (with multiple sublists like this):
['Netherlands', 7, 6, 0, 1, 12, 6, 6, 18]
First it should be sorted according to index 8, then if the numbers for index 8 are the same it should be sorted according to index 7, etcetera
I've managed to sort it according to index 8, but got stuck after that. So I'm using selection sort here:
def Sorting(List, sortVar):
for index in range(len(List)):
cur_pos = stats[index][sortVar]
high = List[index]
loc = index
for i in range(index,len(List)):
if stats[i][sortVar] > cur_pos:
cur_pos = List[i][sortVar]
high = List[i]
loc = i
temp = List[index]
List[index] = high
List[loc] = temp
print("")
print(List)
return List
And then after this I've tried some things but gotten stuck. Might be a really simple question, but I'm really struggling. Thanks!
Edit: I've seen some posts explaining this, but I didn't understand them and they all used inbuilt sorting functions, which I'm not allowed to do...
sorted()/list.sort?sortorsortedwith a tailoredkey?['Netherlands', 7, 6, 0, 1, 12, 6, 6, 18]. The output should be this list sorted first on index 8, then if entries for index 8 are the same for index 1, etcetera. (Sorry hope this is clear, I'm very new to programming) I want to implement a sorting algorithm as the one I put in the original post, which they called: "selection sort" in my lectures. I cannot usesortorsorted. I hope this is a bit clearer @Chris_Rands? So in the end we want to know which country would have the highest rating.