I want to sort a list of session beans by their string session keys so I implemented the following.
If I want to search this sorted list using Collections.binarySearch with the search key as a String and using the same comparator how do I know whether objA or objB will be the search String and which will be the bean.
Will I need to do the following
Hope this makes sense, I know I should be checking the bean class and throwing ClassCastExceptions etc but I've left it out for clarity. [ July 07, 2006: Message edited by: Graham VMead ]
There's no other obvious way to do it; what you've done makes perfect sense.
Regarding type-checking: it's generally considered OK to just cast obliviously in Comparators, without explicit checking. When you call sort() using such a Comparator, then it would make sense to catch ClassCastException.