Lab 4 Comparator Solution

Lab 4

Create a class named LibraryBook that contains fields to hold the title, author, and pagecount. It should have set and get methods for each field.

Write an application that instantiates five LibraryBook objects and prompts the user for values for the data fields. The objects should be stored in either an Array or an ArrayList. Then prompt the user to enter which field the libraryBooks should be sorted by – title, author, or pageCount. Perform the requested sort procedure and display the LibraryBook objects in the sorted order.

Our book has information about sorting an Array using the Array class sort method, which uses a comparator. The ArrayList class also has the ability to use a comparator, though it is set up slightly differently, using the Collections.sort. A quick google search got me these web sites, which seem pretty clear and use something similar to what we did with the Hand class in our Card Game project.

Powered by