0

I am using an ArrayList with book titles and book ratings. How can I change this code to make the bubble sort for alphabetical instead of numeric?

System.out.println("\r" + "In order by rating");
for (int out = 0; out < bookList.size(); out++) {
    for (int in = 0; in < bookList.size() - 1; in++) 
        if (bookList.get(in).getRating() < bookList.get(in + 1).getRating()) {
            Book temp = bookList.get(in);
            bookList.set(in, bookList.get(in+1));
            bookList.set(in+1, temp);        
        }    

        System.out.println(videoList.get(out).getTitle() + "   " + videoList.get(out).getRating());
    }
}

My other classes are below.

Book

public class Book {
    String title;
    int rating;

    public Book(String pTitle, int pRating) {
        title = pTitle;
        rating = pRating;
    }

    public String getTitle() {
        return title;
    }

    public int getRating() {
        return rating;
    }

    public void setTitle(String newTitle) {
        title = newTitle;
    }

    public void setRating(int newRating) {
        rating = newRating;
    }    
}

Library

import java.util.ArrayList;

public class Library {

    public static void main (String [] args) {

        ArrayList<Book> bookList = new ArrayList<Book>();

        Book b1 = new Book ("Huckleberry Finn", 5);
        Book b2 = new Book ("The Great Gadsby", 2);
        Book b3 = new Book ("Harry Potter", 3);
        Book b4 = new Book ("Animal Farm", 4);
        Book b5 = new Book ("The Mist", 1);
        bookList.add(b1);
        bookList.add(b2);
        bookList.add(b3);
        bookList.add(b4);
        bookList.add(b5);
        System.out.println("Original sequence");

        for (int cnt = 0; cnt < videoList.size(); cnt++) {
            System.out.println(bookList.get(cnt).getTitle() + "   " + bookList.get(cnt).getRating());
        }
    }
}

Is there a way to alter the code in the algorithm class to display the bookList sorted by Title?

Dan Oberlam
  • 2,435
  • 9
  • 36
  • 54
D Bowman
  • 71
  • 7

4 Answers4

3

You can't use < directly on two Strings, but you can use compareTo.

if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0) { ...

If s1 and s2 are strings, s1.compareTo(s2) returns a negative value if s1 is lexicographically less than s2, a positive value if s1 is greater, and 0 if the two strings are equal.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • It's not sorting them books. It just prints them in the order they are in the code. – D Bowman Jun 20 '14 at 00:23
  • @user3741689 Then check the rest of your algorithm. The code you posted at the top looks like it might not be your real code, since you're mixing `bookList` and `videoList`. Also you don't wait until everything is sorted before you print it out. If you're having trouble getting it to work, try using a debugger or putting `System.out.println` in your code to print some data out as your algorithm progresses. If you try this and are still totally stuck, please start a new question. – ajb Jun 20 '14 at 00:28
1

For your class Book make it implement Comparable. You'll have to create some methods in your Book class in order to compile. Implement them according to the Java API then you can just throw them into a TreeSet<Book> and it will be sorted.

Edit: I realize this doesn't directly answer your question, but it would be a more Java solution.

Carlos Bribiescas
  • 4,197
  • 9
  • 35
  • 66
1

I think change your code :

if (bookList.get(in).getRating() < bookList.get(in + 1).getRating()) 

to

if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()<0)

would be OK.

But,why dont you implement different Comparators and use it like this: Collections.sort(bookList,yourComparator)

something like this:

public static void main(String[] args) {
    List<Book> bookList = new ArrayList<Book>();
    Collections.sort(bookList, new TitleComparator());
    Collections.sort(bookList, new RatingComparator());
}

static class TitleComparator implements Comparator<Book> {
    @Override
    public int compare(Book o1, Book o2) {
        return o1.getTitle().compareTo(o2.getTitle());
    }
}

static  class RatingComparator implements Comparator<Book> {
    @Override
    public int compare(Book o1, Book o2) {
        return o1.getRating() - o2.getRating();
    }
}
BlackJoker
  • 3,099
  • 2
  • 20
  • 27
  • Use the built-in `sort` in a college course? Ha, ha, ha, ha. (I'm sure that writing the bubble-sort algorithm is part of the homework.) But +1 for demonstrating how to use `Comparator`s. – ajb Jun 19 '14 at 02:02
  • U r doing homework...I think my answer is a more common java solution for this. – BlackJoker Jun 19 '14 at 02:07
0

To implement bubble-sort for any type of Object, in that case Book, implements in your object class the interface Comparable and override the methode compareTo to define your desired handling.

The method should return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

As stated in the javadoc :

http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

In your case, the compareto method is already implemented in the String class so you can directly use it on the book's title.

Your validation would then look like this :

if (bookList.get(in).getTitle().compareTo(bookList.get(in + 1).getTitle()) < 0)

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • We had some discussion just recently, on a different question: http://stackoverflow.com/questions/24271616/equals-method-contract-with-comparable-interface/24271763#24271763. There's room for debate, but some of us feel that a custom `Comparator` (or multiple `Comparator`s) is better than implementing `Comparable`, since there's no one "natural" ordering that stands out. YMMV. – ajb Jun 19 '14 at 02:01