Votre problème avec cette ligne de code. Votre classe n'est pas un Type de Comparable
Alors, Sur quelle propriété ou critère compareTo()
méthode va compare
ces deux objets ???
res = maxtest.compareTo(maxtest2); //Your maxtest object is not Comparable Type.
Vous devez vous rendre votre classe Term
de type Comparable. et, Remplacez la méthode compareTo()
selon votre besoin. Vous n'avez pas mentionné la variable ou la structure de votre classe Term
. Donc, je suppose que votre classe a ce genre de structure.
public class Term implements Comparable<Term> {
private Character alpha;
private int number;
//getter and setters +Constructors as you specified
....
....
...
.....
// Now Set a criteria to sort is the Alphanumeric.
@Override
public int compareTo(Term prm_obj) {
if (prm_obj.getAlpha() > this.alpha) {
return 1;
} else if (prm_obj.getAlpha() < this.alpha) {
return -1;
} else {
return 0;
}
}
Maintenant votre classe devient un type comparable
. Donc, vous pouvez appliquer Collections.sort(Collection obj)
qui automatiquement sort
votre ArrayList<Term>
.
Ici, j'écris une démo pour cela.
public static void main(String... args){
List<Term> obj_listTerm = new ArrayList<>();
//add all the data you given in question
obj_listTerm .add(new Term('Z', 4));
obj_listTerm .add(new Term('Q', 2));
obj_listTerm .add(new Term('c', 3));
// print without Sorting your Term ArrayList.
System.out.println("This is the list unsorted: " + myTermList);
// Sort Using Collections.sort() Method.
Collections.sort(myTermList);
// After applying sort() you may see your Sorted ArrayList.
System.out.println("This is the list SORTED: " + myTermList);
}
Got it :) Merci! – Alfred