I have an ArrayList which I need to sort and also I need a specific default item to be on top of the list. I can do that myself by checking the array list, deleting the default item, and inserting it at the top of the list.
Basically the list should have a default value on top and remaining values in sorted order. Is there is any existing API method that does this?
I’d just write a method that did what you describe… something like:
You could also make use of a Comparator (it would be less obvious though) where it always compares the default item as the lowest in any comparison and the natural comparison for the rest of the items. For that you would call Collections.sort(List, Comparator);
Here is some code for such a Comparator… again. I don’t really like it because it isn’t obvious… but it is a reasonable solution I guess: