i want to do nested sorting . I have a course object which has a set of applications .Applications have attributes like time and priority. Now i want to sort them according to the priority first and within priority i want to sort them by time.
You've already asked this question elsewhere. Write an implementation of java.util.Comparator.
Take a look at the Google Collections Ordering class at http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?com/google/common/collect/Ordering.html. It should have everything you need plus more. In particular you should take a look at the compound method to get your second ordering.
For example, given this class (public fields only for brevity):
public class Job {
public int prio;
public int timeElapsed;
}
you might implement sorting by time using the static sort(List, Comparator) method in the java.util.Collections class. Here, an anonymous inner class is created to implemented the Comparator for "Job". This is sometimes referred to as an alternative to function pointers (since Java does not have those).
public void sortByTime() {
AbstractList<Job> list = new ArrayList<Job>();
//add some items
Collections.sort(list, new Comparator<Job>() {
public int compare(Job j1, Job j2) {
return j1.timeElapsed - j2.timeElapsed;
}
});
}
Mind the contract model of the compare() method: http://java.sun.com/javase/6/docs/api/java/util/Comparator.html#compare(T,%20T)
To sort on multiple criteria, a couple of common approches using the Comparable interface:
- write your compareTo() method so that it compares one field, and then goes on to compare the other if it can't return an ordering based on the first;
- if you're careful then again, in your compareTo() method, you can translate a combination of both criteria into a single integer that you can then compare.
The first of these approaches is usually preferable and more likely to be correct (even though the code ends up looking a bit more cumbersome).
See the example on my web site of making Java objects sortable, which shows an example of sorting playing cards by suit and number within the suits.
Subtracting the two numbers as in the example above is not always a good idea.
Consider what would happen if you compared -2,147,483,644 with 2,147,483,645. Subtracting them would cause an integer overflow and thus a positive number. A positive number means would cause the comparator to claim that -2,147,483,644 is larger than 2,147,483,645.
-5 - 6 = -7
-2,147,483,644 - 2,147,483,645 = 1
Subtracting to find the compare value is even more dangerous when you consider comparing longs or doubles, since that have to be cast back to ints providing another opportunity for an overflow. For example, never do this:
class ZardozComparorator implements Comparator<Zardoz>{
public int compare(Zardoz z1, Zardoz z2) {
Long z1long = Long.getLong(z1.getName());
Long z2long = Long.getLong(z2.getName());
return (int)(z1long-z2long);
}
}
Instead use the compare method of the object you are comparing. That way you can avoid overflows and if needed you can override the compare method.
class ZardozComparorator implements Comparator<Zardoz>{
public int compare(Zardoz z1, Zardoz z2) {
Long z1long = Long.getLong(z1.getName());
Long z2long = Long.getLong(z2.getName());
return z1long.compareTo(z2long);
}
}