-
array sort, comparable?
I'm interested in using the sort method of the array class.
sort(Object[] a, Comparator c)
I have anarray of ADTs cal them X[]. This type has many public varibles in the class. I'd like to sort the array by these varibles, this possible with X.sort(X, ???);?
Any other ideas? Or am i going to have to write the sorting method??? :(
Thanks!
NOMAD
-
Two options:
1) Let X implement Comparable, implement compare and use the sort function without a Comparator argument.
2) Don't implement Comparable, instead write a custom class that implements Comparator (could be an anonymous class) and pass it as Comparator argument.
-
Good ideas
Thanks Corned Bee, I like idea #2. :D
NOMAD
-
Depending on your data type #1 might make more sense. For example I explained a guy in the C++ forum today how to sort highscores using a std::set (similar to java.util.TreeSet). A highscore type should be Comparable.