Google Guava is a java library with lot of utilities and reusable components.
This requires the library guava-10.0.jar to be in classpath.
The following example shows using Ordering.from() and
Ordering.compound() APIs.
It allows us to perform multi level ordering.
List<Bug> bugs = Lists.newArrayList( new Bug("Bug1", "Open", 3), new Bug("Bug2", "Resolved", 3), new Bug("Bug3", "Closed", 1), new Bug("Bug4", "Open", 4), new Bug("Bug5", "Resolved", 2), new Bug("Bug6", "Open", 1), new Bug("Bug7", "Closed", 3), new Bug("Bug8", "Resolved", 1)
);
Comparator<Bug> statusComp = new Comparator<Bug>() {
@Override public int compare(Bug b1, Bug b2) { return b1.getStatus().compareToIgnoreCase(b2.getStatus());
}
};
Comparator<Bug> priorityComp = new Comparator<Bug>() {
@Override public int compare(Bug b1, Bug b2) { return Ints.compare(b1.getPriority(), b2.getPriority());
}
};
System.out.println("Actual Bug List : ");
showBugList(bugs);
//Order by status and then priority
List<Bug> sortedBugs = Ordering.from(statusComp).compound(priorityComp).sortedCopy(bugs);
System.out.println("\nSorted Bug List : ");
showBugList(sortedBugs);
}