1

I have an assignment that I am currently stuck on. I have a SchoolTextBook class that includes getters/setters for each data member. I have to create the objects in a SchoolTextBook sort class, load them into an array, and ask the user how they want the information sorted. I have everything else sorted except for the Price data member. I can't for the life of me figure out how to pull the double value out of the object in the array, and sort the array's contents according to the value. I'm having a similar problem with the int PageCount, too.

I've tried the sort methods in Array. However I can't find a way to properly compare the double values.

public class SchoolTextBookSort {

public static void main(String [] args)
{
    char choice;
    SchoolTextBook[] books = new SchoolTextBook[5];

    SchoolTextBook b1 = new SchoolTextBook("Android Bootcamp", "Corinne Hoisington", 500, "9781305857995", 53.87);

    SchoolTextBook b2 = new SchoolTextBook("Linux Essentials", "Christine Bresnahan", 342, "9871119092063", 31.93);

    SchoolTextBook b3 = new SchoolTextBook("Murach's MySQL", "Joel Murach", 590, "9781890774820", 57.50);

    SchoolTextBook b4 = new SchoolTextBook("Java Programming", "Joyce Farrell", 994, "9781285856919", 52.00);

    SchoolTextBook b5 = new SchoolTextBook("C# Programming", "Barbara Doyle", 1156, "9781285856872", 91.69);

    books[0] = b1;
    books[1] = b2;
    books[2] = b3;
    books[3] = b4;
    books[4] = b5;

    greeting();
    choice = getChoice();

    switch(choice)
    {
    case 'A':
        sortTitle(books);
        break;
    case 'B':
        sortAuthor(books);
        break;
    case 'C':
        sortPages(books);
        break;
    case 'D':
        sortPrice(books);
        break;
    case 'E':
        sortISBN(books);
        break;
    default:
        System.out.println("The books we currently have on-hand:");
        System.out.println(b1.toString() + "\n" + b2.toString() + "\n" 
                    + b3.toString() + "\n" + b4.toString() + "\n" + b5.toString());
        break;
    }

}

public static void greeting()
{
    System.out.println("School Textbook Program\n\n");
}

public static char getChoice()
{
    String choice = "";
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the character corresponsing to the type of sort you want:");
    System.out.println("A: Alphabetically by title\nB: Alphabetically by Author" 
    + "\nC: Least to most pages\nD: Book price -- low to high\nE: ISBN");
    System.out.print("\nPlease enter your choice: ");
    choice = input.nextLine();

    choice = choice.toUpperCase();

    char character = choice.charAt(0);

    return character;
}

public static void sortTitle(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getTitle().compareTo(b.getTitle()));
    System.out.println(arr1);
}

public static void sortAuthor(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getAuthor().compareTo(b.getAuthor()));
    System.out.println(arr1);
}

public static void sortPages(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getPageCount() - b.getPageCount());

}

public static void sortISBN(SchoolTextBook arr1[])
{
    Arrays.sort(arr1, (a, b) -> a.getISBN().compareTo(b.getISBN()));    
    System.out.println(arr1);
}

public static void sortPrice(SchoolTextBook arr1[])
{
    //Tried something like the sortPages method but
   //it didn't work for either method

}
Kayla
  • 11
  • 3
  • `Arrays.sort(arr1, (a, b) -> Double.compare(a.getPrice(), b.getPrice()));` – Andreas Jul 19 '19 at 20:36
  • Also, in Java 8+, `sortTitle` could be `Arrays.sort(arr1, Comparator.comparing(SchoolTextBook::getTitle));` and `sortPrice` could be `Arrays.sort(arr1, Comparator.comparingDouble(SchoolTextBook::getPrice));` – Andreas Jul 19 '19 at 20:41

0 Answers0