How to sort java.util.ArrayList <ParentType> based on ChildType?
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...) } Output
Men (A); Men (C); Men (E); Me not); Men (X); Men (Z) Women (A); Women (B); Women (G); Women (AJ);
My sorted arraylist should be initially sorted in terms of the TYPE (Men or Women) and the secondary sort is based on firstname . How can I do this best?
I tried Collections.sort(....)
Failed to get the desired results.
Thank you in advance
You probably mean
public interface Human extends Comparable<Human> {} That is: a person is comparable to other people. In this case, if you want to compare people based on the type followed by the name, then your Human interface should express both of these properties:
public interface Human extends Comparable<Human> { enum Type { MAN, WOMAN } Type getType(); String getName(); } Then write the appropriate implementation of compareTo() to take into account both type and name, and use Collections.sort() to sort.
You need to use comparator and 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); Further information can be found in the Object Ordering Guide: http://download.oracle.com/javase/tutorial/collections/interfaces/order.html
Comparator implementation based on Ryan Stewart solution . It works great!
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()); } } }); 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; } }
and you can sort the collection using the following command:
Collections.sort(engineers ,new HumanComparator()); Do you guys recommend this?
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; } } .... and add more code to further sort by name ...