//For Class A
public class NameCompare implements Comparator<A>
{
public int compare(A a1, A a2){
if(a1.findName()> a2.findName()){
return 1;
}
else if(a1.findName()== a1.findName())
return 0;
else {
return -1;
}
}
}
//For Class B
public class ACompare implements Comparator<B>
{
public int compare(B b1, B b2){
if(b1.getId()> b2.getId()){
return 1;
}
else if((b1.getId()== (b2.getId()())
return 0;
else {
return -1;
}
}
}
//Main Class
ArrayList<A> copy = (ArrayList<A>)B.getA();
Collections.sort(copy, new NameCompare());
Collections.sort(copy, new ACompare());
for (A a : copy) {
System.out.println(a);
}
I am new to Java. As far as I know, the method above may be an incorrect way to sort a list by using the attributes from 2 different classes. Assuming class B holds an array list of class A. How would I sort the list, lets say I would want to display an ascending id, then the name