I have a arraylist of a POJO and the data in it is of the form
id time
2 467
3 403
4 602
3 529
5 398
The requirement is that first I need to sort the data by time and then after that the same IDs should be one after the other i.e
id time
5 398
3 403
3 529
2 467
4 602.
Initially to sort by time , I’m using the following logic
Collections.sort(list, new Comparator<Asset>() {
@Override
public int compare(Asset o1, Asset o2) {
if (o1.getTime() > o2.getTime())
return -1;
else if (o1.getTime() < o2.getTime())
return 1;
else
return 0;
}
});
Could some one help me in clubbing by IDs in the next stage?
To sort the data according to the example you gave, you probably need two passes over the list. (How else would you determine whether or not
3 504should come before or after5 315?)Here’s some sample code:
Output: