Sorting

Your programs can sort lists of values in different ways. Perhaps the list was sorted previously, and now you want to reverse the sort. A reversed sort may be appropriate, for example, to print customer balances from the highest balance to the lowest.

Consider the following list of numbers that is not sorted:

10 54 34 46 23

Here is the list sorted in ascending order (from lowest to highest):

10 23 34 46 54

Here is the list sorted in descending order (from highest to lowest):

54 46 34 23 10

Of course, your program can also sort character string data, such as a list of names. When sorting strings, the computer uses the ASCII table to sort the values in alphabetical order.

There are several ways to sort data. The most popular one for beginning ...

Get Absolute Beginner's Guide to Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.