O'Reilly logo

Head First Learn to Code by Eric Freeman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7. 4, part 2, sorting and nested iteration: Putting Some Order in Your Data

in which we return to the lists and add some superpowers

Image

Sometimes the default ordering of your data doesn’t cut it. You’ve got that list of high scores on 80s arcade games, but you really need it sorted alphabetically by game name. Then there’s that list of the number of times your coworkers have stabbed you in the back—it would be nice to know who’s at the top of that list. To do that, though, we need to learn how to sort data, and to do that we’ll need to explore some algorithms that are a little more involved than the ones we’ve seen so far. We’re also going to have to explore how nested loops work as well as think a little about the efficiency of the code we’re writing. Come on, let’s take that computational thinking up a level!

Image

Cubicle conversation

Image

Frank: Well, it doesn’t sound bad, but how do we do it?

Judy: We’ve been coming up with some great algorithms, so I’m sure we can tackle sorting.

Joe: Actually, sorting algorithms are a topic some computer scientists spend their entire careers researching. They’re not something we should “come up with,” but rather we need to research the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required