Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Complex Sorting

Tags:

java

sorting

I am stuck in that I have an Object Book that got three variables

String title
int Year
String authorName

I must sort the books by one, two or all three of the variables in ascending or descending order, I implemented the title ordering but I am stuck as to what to do when people choose more than one variable to order.

Here is some of my code:

Book Class:

import java.util.ArrayList;


public class Book{

String title;
String authorName;
int editionYear;

public Book(String title, String authorName, int editionYear){
    this.title = title;
    this.authorName = authorName;
    this.editionYear = editionYear;

}

public String getBookInfo(){

    ArrayList bookInfo = new ArrayList();
    bookInfo.add(this.title);
    bookInfo.add(this.authorName);
    bookInfo.add(this.editionYear);
    return bookInfo.toString();
}

}

BookSorter Class:

import java.util.Arrays;
import java.util.Comparator;

public class BookSorter{

private String sortkey;
private String order;
Book[] Books;

public BookSorter(Book Book1, Book Book2, Book Book3, Book Book4){
    this.Books = new Book[] {Book1, Book2, Book3, Book4};
}

public Book[] sortByTitle(boolean sortorder){
    Comparator<Book> byTitle = new TitleComparator(sortorder);
    Arrays.sort(Books, byTitle);
    for(int i=0;i<4;i++) System.out.println(Books[i].title);
    return Books;
}
}

TitleComparator:

import java.util.Comparator;

class TitleComparator implements Comparator<Book> {

boolean ascending;

public TitleComparator(boolean ascending){
    this.ascending = ascending;
}

public int compare(Book Book1, Book Book2){
    if(ascending == true){
        if(Book1.title.compareToIgnoreCase(Book2.title) > 0) return 1;
        else if(Book1.title.compareToIgnoreCase(Book2.title) < 0) return -1;
        else return 0;
    }else{
        if(Book2.title.compareToIgnoreCase(Book1.title) < 0) return -1;
        else if(Book2.title.compareToIgnoreCase(Book1.title) > 0) return 1;
        else return 0;
    }
}
}

I though I could work a little more on the comparator but I am really stuck on how to model such a thing, Thanks in advance

like image 572
bwagner Avatar asked Jan 22 '11 21:01

bwagner


1 Answers

Write 3 comparator classes that each compare a specific attribute and then an overall comparator class that takes an ordered list of comparators.

Or use some a convenience class from a library like org.apache.commons.collections.comparators.ComparatorChain.

Edit:

OP asks:

how could I write that overall comparator:

Something like:

// private List<Comparator<?>> comparators;  // initialized in constructor

// compare method(book1, book2):
//     note that while result == 0, books have had equal attributes so far
//     once result is != 0, the books are now ordered - no need to compare further
//     if we run out of comparators and result still == 0, books are equal.

//     initialize iterator to list of comparators
//     int result = 0;
//     while result == 0 && still more comparators
//         get current comparator from iterator
//         result = comparator.compare(book1, book2); // compare current attribute
//     end-while
//     return result
like image 163
Bert F Avatar answered Oct 16 '22 08:10

Bert F