Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I want to sort my leaderboard

+2 votes
asked Apr 7 by Jezrin Jacob (170 points)
1. CSV file needs to be sorted.

2. Only the top 5 scores should be displayed

A snipet of my code:

file= list(csv.reader(open('Leaderboard.csv')))
print('This is the current leaderboard...')
print ('Name       Score')
tmp1 =[]
for row in file:
    names = str (tmp1[count1][0])
    names1 = str(tmp1[count1][1])
    count1= count1+1
    print (names + '         ' + names1)

The output:

This is the current leaderboard...
Name       Score
jazz         10
kyle         10
leah         13
page         11
roll         16
mate         11
pole         13

1 Answer

0 votes
answered Apr 8 by Peter Minarik (63,180 points)
selected Apr 10 by Jezrin Jacob
Best answer

You should not print out the read (name, score) pairs, but rather store them in a list.

When you have read all the elements, you can sort the list backward.

Look at Example 3 here plus apply reverse = True sorting parameter.

After this, you can take the top 5 elements from your list.

Now it's your turn to get this coded.

Good luck!

commented Apr 10 by Jezrin Jacob (170 points)
Thanks a lot! I'll try that.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.