upanshu vaid wrote:How can I make comparator to sort according to name and according to sex
You were a whisker away from it with the comparator you just wrote, but then you returned 'name'.
As K.Tsang said, if you want to order by name THEN sex, then the sex comparison only needs to be done if the names are equal.
The problem is that, for any Comparable object, there are TWO ways of determining if they are "equal":
1. equals() returns true.
2. compareTo() returns 0.
And there is nothing that requires that the two methods work the same (although it's usually a good idea if they do).
Since your Comparator is creating an order, it makes sense to me to use compareTo(), since it is the method that provides an order; so putting all that together, you get (from your own method):or something very like it.
See how close you were?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss