Menu
Coddy logo textTech

Recap - Custom Sorting

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 46 of 87.

challenge icon

Challenge

Easy

Let's build a book collection manager that brings together everything you've learned about special methods and sorting. You'll create a Book class with proper toString(), equals(), hashCode(), and natural ordering via Comparable, plus external comparators for flexible sorting options.

You'll organize your code across four files:

  • Book.java: Create a class representing a book with three private fields: title (String), author (String), and year (int). Include a constructor and getters for all fields.

    Your Book should implement Comparable<Book> with natural ordering by year in ascending order (oldest first).

    Override toString() to return: [title] by [author] ([year])

    Override equals() so two books are equal when they have the same title AND author. Override hashCode() using Objects.hash() with the same fields.

  • TitleComparator.java: Create a comparator that sorts books alphabetically by title in ascending order (A to Z).
  • AuthorComparator.java: Create a comparator that sorts books alphabetically by author in ascending order (A to Z).
  • Main.java: Bring everything together! You'll receive input for three books, each on a separate line in the format: title,author,year

    Create an ArrayList of books and demonstrate all three sorting approaches:

    1. Sort using the natural ordering (by year) and print each book
    2. Print an empty line
    3. Sort by title using TitleComparator and print each book
    4. Print an empty line
    5. Sort by author using AuthorComparator and print each book

You will receive three lines of input, each containing book data in the format: title,author,year

For example: 1984,George Orwell,1949

Remember to import java.util.Objects in Book.java, java.util.Comparator in your comparator files, and java.util.ArrayList, java.util.Collections, and java.util.Scanner in Main.java.

Try it yourself

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Read three books from input
        String line1 = scanner.nextLine();
        String line2 = scanner.nextLine();
        String line3 = scanner.nextLine();
        
        // TODO: Parse each line to extract title, author, and year
        // Hint: Use split(",") to separate the values
        
        // TODO: Create Book objects from the parsed data
        
        // TODO: Create an ArrayList and add all three books
        
        // TODO: Sort using natural ordering (by year) and print each book
        
        // TODO: Print an empty line
        
        // TODO: Sort by title using TitleComparator and print each book
        
        // TODO: Print an empty line
        
        // TODO: Sort by author using AuthorComparator and print each book
    }
}

All lessons in Object Oriented Programming