so i need help on Modifying the source code to sort on the Title (in ascending order) and display the book title, comma space, author’s first name, comma space, author’s last name. Ensure all your variable names are appropriately named.

and i also need help Modifying the source code to display and sort on the Title in reverse

any help would be welcome

{
      // Declarations
      Book[] bkarray = new Book[4];
      String firstName, lastName, title;
      BufferedReader in=new BufferedReader(
                            new InputStreamReader(System.in));

      // Loop to get information on the 5 books
      for (int i=0; i<4; i++)
         {
            // Prompts user questions
            System.out.print("Please enter author #"+(i+1)+
                 "'s first name: ");
            firstName = in.readLine();
            System.out.print("Please enter author #"+(i+1)+
                 "'s last name: ");
            lastName = in.readLine();
            System.out.print("Please enter author #"+(i+1)+
                 "'s book title: ");
            title = in.readLine();
            bkarray[i] = new Book();
            bkarray[i].setFirstName(firstName);
            bkarray[i].setLastName(lastName);
            bkarray[i].setTitle(title);
         }

      // Loop to sort the books by Last Name
      for (int i=1; i<4; i++)
         {
            int j=i;
            String tempLN = bkarray[i].getTitle();
            Book tempBk = bkarray[i];
            while ((j>0) &&
             (bkarray[j-1].getTitle().compareTo(tempLN)>0))
            {
               bkarray[j] = bkarray[j-1];
               j--;
            }
         bkarray[j] = tempBk;
         }
      // Loop to Print results to the screen
      for (int i=0; i<4; i++)
         {
            System.out.print(bkarray[i].getLastName()+", ");
            System.out.print(bkarray[i].getFirstName()+", ");
            System.out.println(bkarray[i].getTitle());
         }
   }
}

The toString method does not get overridden enough, and is good for displaying your books. Just override your own version which displays the variables you want displayed. Then you can simply pass the object you want displayed directly to System.out.println(obj) and it will print in string form the object. Here is an example:

    //A snippet from a different implementation of a book class
    public String toString(){
        return super.toString() +
            "Author:  " + author + "\n";
    }//end method

There are built in methods for sorting arrays of objects that implement the Comparable interface. In order to use this interface you need to change the book class to implement this interface, and implement the method compareTo. Here is a link to the Comparable interface:
Click Here

import Java.Util.Arrays;
//sort regular order
Arrays.sort(array);
//sort reverse order
Arrays.sort(array, Collections.reverseOrder());

Edited 2 Years Ago by overwraith: more clairity

This article has been dead for over six months. Start a new discussion instead.