Hi i just, got an error with this code; I'm trying to use sort from Collection.
I can't see where is the problem... HELP!
/** From Main: user.messages() returns an ArrayList<Message> */ List<Message> mex = user.messages(); List<Message> l = Collections.sort(mex);
import java.util.*; public class Message implements Comparator<Message>{ private String text; public Message(String text){ this.text = text; } public String toString(){ return this.text; } public int nChar(){ return text.length(); } public boolean equals(Message mesEs){ return (this.text.equals(mesEs.text)) ? true : false; } public int compareTo(Message b){ if(text.compareTo(b.text)<0) return -1; else if(text.compareTo(b.text)>0) return 1; else return 0; } public int compare(Message a,Message b){ return a.compareTo(b); } }
Error: List<Message> l = Collections.sort(mex);
-----------------------------------------------^
method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
method Collections.<T#2>sort(List<T#2>) is not applicable
(inferred type does not conform to declared bound(s)
inferred: Message
bound(s): Comparable<? super Message>)
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)