Search code examples
javasortingbubble-sortcompareto

Can't get bubble sort to work w/ comparing object properties


For an array of books, the assignment is to get attributes of the objects from the user, then sort based on their choice of author, title, or page count. My bubble sort is working for the page count but not when the user chooses author or title, so I'm guessing I'm missing something with reference storage vs. value storage, maybe? Thanks!

import javax.swing.*;
import java.util.*;
public class LibraryBookSort {

public static void main(String[] args) {
    LibraryBook[] books = new LibraryBook[5];
    LibraryBook tempBook = new LibraryBook();
    String enteredAuthor = "", enteredTitle = "";
    String enteredPageCount;
    String sortBy;
    String displayString = "";
    int parsedSortBy;
    int parsedPageCount;
    int booksLength = books.length;

    // populate the array
    for (int x = 0; x < booksLength; ++x)
        books[x] = new LibraryBook();

    // get property values from user
    for (int x = 0; x < booksLength; ++x)
    {
        enteredTitle = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's title:");
        books[x].setTitle(enteredTitle);
        enteredAuthor = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's author:");
        books[x].setAuthor(enteredAuthor);
        enteredPageCount = JOptionPane.showInputDialog(null, "Enter book " + (x + 1) + " of 5's page count:");
        parsedPageCount = Integer.parseInt(enteredPageCount);
        books[x].setPageCount(parsedPageCount);
    }

    // sort by property values
    sortBy = JOptionPane.showInputDialog("Choose option to sort by: (1) title, (2) author, or (3) page count");
    parsedSortBy = Integer.parseInt(sortBy);

    while (parsedSortBy < 1 || parsedSortBy > 3)
    {
        sortBy = JOptionPane.showInputDialog("Invalid selection, please choose option to sort by: (1) title, (2) author, or (3) page count");
        parsedSortBy = Integer.parseInt(sortBy);
    }

    if (parsedSortBy == 1)
    {
        for (int a = 0; a < booksLength - 1; ++a)
        {
            for (int b = 0; b < booksLength - 1; ++b)
            {
                if (books[b].getTitle().compareTo(books[b+1].getTitle()) > 1)
                {
                    tempBook = books[b];
                    books[b] = books[b+1];
                    books[b+1] = tempBook;
                }
            }
        }
    }
    else if (parsedSortBy == 2)
    {
        for (int a = 0; a < booksLength - 1; ++a)
        {
            for (int b = 0; b < booksLength - 1; ++b)
            {
                if (books[b].getAuthor().compareTo(books[b+1].getAuthor()) > 1)
                {
                    tempBook = books[b];
                    books[b] = books[b+1];
                    books[b+1] = tempBook;
                }
            }
        }
    }
    else
    {
        for (int a = 0; a < booksLength - 1; ++a)
        {
            for (int b = 0; b < booksLength - 1; ++b)
            {   
                if (books[b].getPageCount() > books[b+1].getPageCount())
                {
                    tempBook = books[b];
                    books[b] = books[b+1];
                    books[b+1] = tempBook;
                }
            }
        }
    }

    for (int i = 0; i < booksLength; ++i)
    {
        displayString += (books[i].getTitle() + ", by " + books[i].getAuthor() + ". " + books[i].getPageCount() + " pages.\n");
    }
    JOptionPane.showMessageDialog(null, "Books sorted by your choice:\n\n" + displayString);
}

}


Solution

  • The comparison should be

    .... > 0
    

    Not

    .... > 1
    

    The contract of compareTo is to return zero on equal and something >0 for "larger" and <0 for "smaller". In practice the values are -1, 0 and +1.