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

+4
source share
6 answers

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.

+3
source

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

+2
source

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()); } } }); 
+2
source
 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()); 
+2
source

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 ...

+1
source

I recommend using the Comparator interface.
Write another class that implements this interface.
Then, in the comparison method, write the appropriate code that validates the instance, and then the member of the instance.

0
source

All Articles