1

I have Library class that creates a map of either Books objects or Author objects and I need to be able to sort the map using the Book objects attributes. The map is used to create two JTable's one for books and another for authors and I need to be able to sort the columns that are book attributes in the book table. I have to use the map and can't just use JTable sorter btw. Creating the map using bookIndex as the key sorts the map correctly for the bookIndex but this is the natural order.

How can I sort the map that is already created by the Book objects other attributes. For example if i wanted it to sort the existing map based on bookTitle?

I know I need to use a comparator but I have searched and tried and cannot figure this out.

Main (creates library objects and creates and adds the book and author objects to the library map):

    static Library<Book> bookLibrary = new Library<Book>();
    Book book = new Book();
        book.setBookIndex(Integer.parseInt(ar[1]));
        book.setTitle(ar[2]);
        book.setGenre(ar[3]);
        book.setPrice(ar[4]);
        book.setAuthorIndex(Integer.parseInt(ar[5]));

    bookLibrary.add(book.getBookIndex(), book); //adds the book object to library


    static Library<Author> authorLibrary = new Library<Author>();
    Author author = new Author();
                author.setAuthorIndex(Integer.parseInt(ar[1]));
                author.setName(ar[2]);
                author.setStreetAddress(ar[3]);
                author.setCity(ar[4]);
                author.setState(ar[5]);
                author.setZip(ar[6]);
                author.setPhone(ar[7]);

    authorLibrary.add(author.getAuthorIndex(), author); //adds the author object to the library

Book Class (Author Class is simaler to this):

    public class Book implements BookInterface {

        private Integer bookIndex;
        private String title, genre, price;
        private Integer authorIndex;

       ....
    }

Library Class:

    public class Library<T> implements LibraryInterface<T> {
        private Map<Integer, T> map = new TreeMap<Integer, T>();
        ...
    }

. . .

For future reference here is how I got it to work:

    public class Library<T> implements LibraryInterface<T> {
        private Map<String, T> map = new TreeMap<String, T>(new bookComparator());

And the new class:

    public class bookComparator implements Comparator<Object> {

@Override
public int compare(Object o1, Object o2) {
    String bookTitle1 = (String) o1;
    String bookTitle2 = (String) o2;
    int res = bookTitle1.compareToIgnoreCase(bookTitle2);;
    return res != 0 ? res : 1;

        }
    }

And creating a sort method in main class that create a new TreeMap. I would create a copy of the original map and replace the original but it works for now:

    /**
 * Sort The Book Table by Book Title
 * 
 * @param bookLibrary2
 */
private void sortByBookTitle() {
    bookRowElement.clear();
    Library<Book> sortBookLibrary = new Library<Book>();
    for ( Entry<String, Book> entry: bookLibrary.entrySet()) {

        String key = entry.getValue().getTitle();  //Additional sorts can be mad by changing .getTitle() to getWhatever()
        Book value = entry.getValue();
        sortBookLibrary.add(key, value);
    }
    for ( Entry<String, Book> entry: sortBookLibrary.entrySet()) {
        bookRowElement.addElement(createBookSearchList(entry.getValue()));
    }
    bookTable.repaint();
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1677657
  • 89
  • 1
  • 3
  • 13

2 Answers2

1

in your situation, I would just create new TreeMap with given comparator and copy all elements to the new map. I think you will get your result

Miloš Lukačka
  • 842
  • 1
  • 11
  • 25
1

TreeMap has a constructor where you can pass in your custom Comparator. I guess that will do the job for you.

public TreeMap(Comparator<? super K> comparator)

You can have your class like this:

public class Library<T> implements LibraryInterface<T> {
    private Map<Integer, T> map = new TreeMap<Integer, T>(myComparator);
    // ...
}
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • this is where i am stuck. I cant figure out how to create myComparator so that the map is created based on the attributes i want to compare. – user1677657 Apr 15 '13 at 15:06
  • See http://www.techcubetalk.com/2010/08/java-comparator-example/ OR http://stackoverflow.com/a/5245214/548225 for examples on how to create your custom Comparator. – anubhava Apr 15 '13 at 15:10