Page 193 -
P. 193

sort the surfers


          The data isn't sorted


          Your program now associates surfers and their scores, but         Surf-A-Thon
           it displays the data from the hash in some sort of random
           ordering. You need to somehow sort the data in the hash
           to find out who actually won the contest.                    1.      Joseph        8.45
                                                                         2.     Zack          7.21

                                                                         3.     Juan          9.12
           Python hashes don't have a sort() method...

           In Python, hashes are optimized for speedy insertions and even             These scores aren't
           speedier look-ups (searches). As a consequence, the good folks that
           created Python were less interested in providing a method to sort a        in the right order.
           hash, so they didn’t bother.

                                                                 You'll find similar design and implementation
                                                                 decisions in lots of different programming
                                                                 languages. People are different... and so are
          ...but there is a function called sorted()             the programming languages they create.

           Obviously, there’s a need to sort a hash so, again, the good folks that
           created Python decided to provide a really smart built-in function
           that has the ability to sort any data structure, and they called their
           function sorted(). Here’s how to use the sorted() function
           with your hash:
                                                                              Remember: the keys in your hash are
                                                                              the scores, which are numbers, so we ask
                                                                              “sorted()" to order them highest-to-
            Use the “sorted()" function to sort the
            keys of the “scores" hash.                                        lowest using “reverse = True".

                  for each_score in sorted(scores.keys(), reverse = True):
                      print('Surfer ' + scores[each_score] + ' scored ' + each_score)




                                                                                                Do this!
           That’s one small change to one line at the bottom of your program.
           So, let’s go ahead and make that change. Now that you are
           sorting the keys of the hash (which represent the surfer’s scores), it
           should be clear why the scores were used as the key when adding
           data into the hash: you need to sort the scores, not the surfer names,   Make the change to your
           so the scores need to be on the left side of the hash (because that’s    code to use sorted().
           what the built-in sorted() function works with).

           158    Chapter 5
   188   189   190   191   192   193   194   195   196   197   198