i am not clear about the difference between the compare() method and compareTo(); method,
what arguments it ttakes?
as far as i studied i have not understood the point that
compareto()--- we must modify the class whose instance we want to sort!! what does this mean?
and compare()---- we build a class seperate from the class whose instatnce we want to sort.! what does this mean?
can please give a good example to implement them?will be very helpful in understanding!
You got it. Here's an example to clarify your confusion.
vijay umar
Ranch Hand
Joined: Mar 24, 2009
Posts: 100
posted
0
Thanks a lot ... nice exxample!
thus what i understood from the example is if we use comparator we need to do it with sepaerate class and if we use comparable we have to use in the same class which needs to be sorted? am i correct?
Then which is more advantageous?which should be used?
vijay umar wrote:thus what i understood from the example is if we use comparator we need to do it with sepaerate class and if we use comparable we have to use in the same class which needs to be sorted? am i correct?
Then which is more advantageous?which should be used?
Using Comparator<V> gives you more flexibility (i.e: ability to sort the objects of the same class in any number of ways you want).