Hi to all, this is my first participate in this website.I face some problems in implementing the method of the sort. I should implement the (compareTo method) to identify the criterion of sorting. But i doubt with my implementation:I don't know if it's correct or not:
moon Code:
Original
- moon Code |
|
|
|
public int compareTo(Object o) {
Book other = (Book) o;
if (this.serialNumber > other.serialNumber
|| (this.title).compareToIgnoreCase(other.title) > 0
|| (this.author).compareToIgnoreCase(other.author) > 0
|| (this.publisher).compareToIgnoreCase(other.publish er) > 0
|| this.publishingDate > other.publishingDate
|| this.price > other.price)
return 1;
if (this.serialNumber < other.serialNumber
|| (this.title).compareToIgnoreCase(other.title) < 0
|| (this.author).compareToIgnoreCase(other.author) < 0
|| (this.publisher).compareToIgnoreCase(other.publish er) < 0
|| this.publishingDate > other.publishingDate
|| this.price > other.price)
return -1;
return 0;
}
on the other hand, I have to implement in the Graphical User Interface application the list of the ceteria by using combo box. I found it difficult for me to write the sort method using compareTo method and how to activate the selection part in the combo box.for example, Choose a criterion for sort and sort accordingly. For example, the user may choose to sort according to serial number, title, publisher, publishing year, or price by the combo box.
the note, all this methods done by the Linked Class
import java.util.Iterator;
Linked Class Code:
Original
- Linked Class Code |
|
|
|
public class Linked<E> {
boolean empty(){
return true;
}
E head(){
throw new RuntimeException();
}
Linked<E> tail(){
throw new RuntimeException();
}
Linked<E> add(E head){
return new LinkedNode<E>(head, this);//l=l.add("Alia");{Alia{Aisha{}}}
}
int size(){
return 0;
}
public void print(String s){
System.out.print(s);
System.out.println("Empty List");
}
// the method in the LinkedNode can work here
public Linked<E> reverse(){
return this;
}
/*public Linked<E> add(E t){
Linked<E> m = new BookList<E>(head, this);
return m;
}*/
public Linked<E> addTail(E t){
return add(t);
}
public Linked<E> rev(){
return this;
}
public Linked<E> removeTail(){
throw new RuntimeException();
}
public Linked<E> join(Linked<E> list){
return list;
}
public Linked<E> sort(){//less(recursion)+head+tail(recursion)
return this;
}
public Iterator<E> iterator(){
Iterator<E> it = new LinkedIterator<E>(this);
return it;
}
public Linked<E> remove(E item){
throw new RuntimeException();
}
public Linked<E> clear (){
return new Linked<E>();
}
public E getLast(){
throw new RuntimeException();
}
}
other is LinkedNode Class which contain the sort method which I want to modefy it to fit the all creiteria
Sort method Code:
Original
- Sort method Code |
|
|
|
public Linked<E> sort() {// less(recursion)+head+tail(recursion)
Comparable h = (Comparable) head;
Linked<E> less = new Linked<E>();
Linked<E> more = new Linked<E>();
for (Linked<E> l = tail; !l.empty(); l = l.tail()) {
if (h.compareTo(l.head()) > 0) {
less = less.add(l.head());
} else {
more = more.add(l.head());
}
}
less = less.sort();
more = more.sort();
return less.join(more.add(head));
}
Could any one help me very soon because this is a part of the project.
I appreciate your helps.