14.8 Sorted Linked Lists

Let’s go back to our linked list of video game players. If we want to display that list on a website so that all the players can see it, we might want to display the list in ascending (or descending) order by id number, or in alphabetical order by name or game. If we store the items in the list in sorted order, we can display the list by simply calling the toString method.

The items can be sorted based on the values of one of their instance variables. Often, but not always, a class is designed so that one of the instance variables uniquely identifies an object: that instance variable is called a key. For the Player class, it is reasonable to assign a different id value to every Player object, and designate the id

Get Java Illuminated, 5th 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.