Just a word of caution: using simple subtraction for ints can get very nasty if the ints get quite big. Consider the case where h1.getHeight() is Integer.MIN_VALUE, and h2.getHeight() is positive. The result will overflow, and produce nasty results.
If the chances of this exist, the following is much safer (and also applies to long):
But if you know (and can guarantee) that the int values won't get that big, subtracting is not a problem.
Thanks for the information. The above example has height in ascending order and the name is also in the ascending order.
My requirement is : Height should be in the ascending order and name should be in descending order.
Thanks
Sudha
sudha swami wrote:Hi, Thanks for the information. The above example has height in ascending order and the name is also in the ascending order.
My requirement is : Height should be in the ascending order and name should be in descending order.
ThanksSudha
I just gave you an example. You'll have to figure out how to apply it to your situation. Experiment a little.
I tried it the same way as you did since last few days but i couldnt figure out whethere there is an option to do one column as ascending and the other one as descending
For descending, all you need to do is flip the sign (e.g. -13 becomes 13, or 481 becomes -481). You can just return -(previous return value) except when the previous return value is Integer.MIN_VALUE.
Rob,
I implemented the code changes according to your suggestion(return -(previous return value) ) and it is working fine.
Thanks for your suggestion. But i dont understand
"You can just return -(previous return value) except when the previous return value is Integer.MIN_VALUE "
Integer.MIN_VALUE is -2,147,483,648, so -Integer.MIN_VALUE would be 2,147,483,648 - that is Integer.MAX_VALUE + 1. Since this is larger than Integer.MAX_VALUE, it wraps and... becomes Integer.MIN_VALUE again!
Byte.MIN_VALUE, Short.MIN_VALUE and Long.MIN_VALUE have the same issue.