I am working on a report module that shows the time spent and number of tasks. The values are set in the Java Bean and the bean object is stored in a array.I am using separate queries to get time and number of tasks.Now i have to sort the array based on time and number of tasks.
The code below compares only Strings:
if (!list.isEmpty()) {
Collections.sort(list, new Comparator<Project>() {
@Override
public int compare(Project p1, Project p2) {
return p1.getName().compare(p2.getName());
}
});
}
I have problems in sorting the integer value of a property in JavaBean which is stored in an array.Any help is greatly appreciated.
In your comments below the question you say you have:
If I were you I’d declare
ProjectasThen you can sort your array by simply calling
If you don’t want your class to implement the
Comparableinterface you can pass a comparator toArrays.sort():I used an anonymous one, you could also extract it to it’s own class if needed elsewhere.