In java I have a custom class, and I sort it like this:
public static void sortList(List<FishCategory> categories) {
Collections.sort(categories, new Comparator<FishCategory>(){
public int compare(FishCategory s1, FishCategory s2) {
return s1.getName().compareTo(s2.getName());
}
});
}
But like sql where you can do this:
select * from mytable
order by id, name
I want to double sort in java. I want to sort by this (Note: im using getParentId
) as the first sort, then I want to sort like above.
public static void sortList(List<FishCategory> categories) {
Collections.sort(categories, new Comparator<FishCategory>(){
public int compare(FishCategory s1, FishCategory s2) {
return s1.getParentId().compareTo(s2.getParentId());
}
});
}
I can't just run both functions one right after the next cause that would cancel out the first sorting. I need to sort the way sql does it (i.e. sort the sorted groups).
So I want to sort by .getParentId()
first, then .getName()
.
Does anyone know a good way to do this easily?
Thanks