public interface Human<T> extends Comparable<T>{ }
public class Men implements Human<Men>{
public Men(String firstName) {
this.firstName = firstName;
}
.....
}
public class Women implements Human<Women>{
public Women(String firstName) {
this.firstName = firstName;
}
.....
}
public class MainTest{
public static void main(String[] args) {
List<Human> engineers= new ArrayList<Human>();
engineers.add(new Men("A"));
engineers.add(new Women("A"));
engineers.add(new Men("C"));
engineers.add(new Men("E"));
engineers.add(new Men("Z"));
engineers.add(new Women("J"));
engineers.add(new Women("B"));
engineers.add(new Men("X"));
engineers.add(new Men("O"));
engineers.add(new Women("G"));
Collections.sort(engineers);
System.out.println(.... print the engineers array...)
}
输出
男性(A);男性(C);男装(E);男性(O);男性(X);男子(Z)女子(A);女性(B); 女性(G);女性(AJ);
我对arraylist的排序应为initially sorted in terms of the TYPE (Men or Women) and the secondary sort is based on firstname
。我怎么能最好地完成这个呢?
我尝试了Collections.sort(....)
无法获得理想的结果。
提前致谢
答案 0 :(得分:3)
你可能意味着
public interface Human extends Comparable<Human> {}
那就是:人类与其他人类相当。既然如此,如果你想根据类型后跟名称来比较人类,那么你的界面Human需要表达这两个属性:
public interface Human extends Comparable<Human> {
enum Type { MAN, WOMAN }
Type getType();
String getName();
}
然后编写适当的compareTo()
实现,同时考虑类型和名称,并使用Collections.sort()
进行排序。
答案 1 :(得分:2)
您需要使用comparator和Collections.sort(List l, Comparator c)
static final Comparator<Human> MyComparator =
new Comparator<Human>()
{
public int compare(Human e1, Human e2)
{
// Your custom comparison code goes here
}
};
Collections.sort(engineers, MyComparator);
可以通过对象排序教程找到其他信息: http://download.oracle.com/javase/tutorial/collections/interfaces/order.html
答案 2 :(得分:2)
比较实施based on Ryan Stewart's solution
。效果很好!
Collections.sort(engineers, new Comparator<Human>() {
@Override
public int compare(Human o1, Human o2) {
if(o1.getType().equals(o2.getType())) {
return o1.getFirstName().compareTo(o2.getFirstName());
} else {
return o1.getType().compareTo(o2.getType());
}
}
});
答案 3 :(得分:2)
class HumanComparator implements Comparator<Human>{
@Override
public int compare(Human humanObj1, Human humanObj2) {
int index;
if(humanObj1 instanceof Men && humanObj2 instanceof Men){
String firstName1 = humanObj1.getFirstName();
String firstName2 = humanObj2.getFirstName();
index = firstName1.compareTo(firstName2);
}else if(humanObj1 instanceof Women && humanObj2 instanceof Women){
String firstName1 = humanObj1.getFirstName();
String firstName2 = humanObj2.getFirstName();
index = firstName1.compareTo(firstName2);
}else if(humanObj1 instanceof Men && humanObj2 instanceof Women){
index =-1;
}else if(humanObj1 instanceof Women && humanObj2 instanceof Men){
index =+1;
}else{
index =0;
}
return index;
}
}
您可以使用以下方式对集合进行排序:
Collections.sort(engineers ,new HumanComparator());
答案 4 :(得分:1)
你们是推荐这个吗?
public class HumanComparator implements java.util.Comparator<HumanBeings> {
@Override
public int compare(HumanBeings o1, HumanBeings o2) {
if(o1 instanceof Men && o2 instanceof Men)
return 0;
else if(o1 instanceof Men && o2 instanceof Women)
return -1;
else if (o1 instanceof Women && o2 instanceof Men)
return 1;
return 0;
}
}
....并添加更多代码以进一步按名字排序......
答案 5 :(得分:0)
我建议使用“比较器”界面 编写一个实现此接口的不同类 然后在compare方法中编写适当的代码,检查实例,然后检查实例的成员。