I have a class
public class StudentVO {
int age;
String name;
}
I used the same class in two different areas. At one place i need to sort based on the age. In another place I need to sort based on the name and in
another place i may need sorting based on both age and name. How can I do this?
If one field I can override compareTo()
.
Is it possible to do this?
Here is the code snippet:
According to your question, it will also work when values of specified field will change. You have only to remember to call
sort
method with this comparator.There is new approach for this in
java-8
see Comparator#comparing and Comparator#thenComparing. All you need is to provide a lamda expression/method reference either toStream#sorted()
orList#sort()
method.For example sorting by one field:
Sorting by a few fields:
In java, you have two main way to compare objects. The first is for the class itself to implement the Comparable interface which will mean only one implementations. The second is to have classes implements Comparator interface. This way, you can have multiple comparators for the same class.
This mean that you could define for exemple 3 diffenrent Comparators on your StudentVo class : one that compare only on the name, another that compare the ages and the last one that both properties.
In your application, you use the implementation that suit you need based on what you want to compare. In one place, you will compare students on age Collections.sort(myStudents , new CompareStudentOnAge()). In another place, you use another implementation.
You can find some explanations in this blog post : http://javarevisited.blogspot.fr/2011/06/comparator-and-comparable-in-java.html
I recently had to solve this problem, too. Not sure if this is exactly the same scenario as yours, but I had to write an in-memory sort for zero or more columns of a grid, handwaving over OOM conditions, etc, because my problem was very limited in scope.
I wrote a comparator for each column and a comparator that took a list of comparators. As I identified which columns needed to be sorted and in what order, I added an instance of the corresponding comparator to the list of comparators. Then, use the chained comparator to execute the actual sort.
So, something like this for each comparator:
This for the chained comparator:
Left to your imagination is parsing the sorts and building the chained comparator. I actually made this a bit more complicated because I also incorporated a direction which I implemented by swapping the order of parameters in the call to the sub-comparator in the chained comparator as needed.
1)You should write two Comparator for sorting on age and name separately, and then use the Collections.sort(List,Comparator). Something like this:
And then use them, To sort based on
age
:to sort based on
name
:2) If you think that the
List
ofStudentVO
has some natural order of sorting, say suppose sort byage
. Then, use Comparable forage
andComparator
forname
.And then use them, To sort based on
age
:to sort based on
name
: