Nice explanation, but at the end, s1 and s2 represent the two arguments passed to that method. It doesn't matter where they come from; remember methods don't record the source of their arguments. If you use Collections#sort() or Arrays#sort(), you do not know the order that pairs are compared in; the algorithm used varies depending on the type of array.Puspender Tanwar wrote:. . .
s1 and s2 represent the current and next element in the List. s1 is the current element and s2 is the element next to s2. this is used for comparing the adjacent elements.
Hope it helps
Campbell Ritchie wrote:When it comes to interview questions . . .
kiran madhan wrote:. . . what i told is "comparable helps us to compare objects based on the comparator defined by comparator."
Campbell Ritchie wrote:. . . Have you worked out how to create a Comparator from a λ?
Campbell Ritchie wrote:Nice explanation.........Please explain why the minus sign is error‑prone, Have you worked out how to create a Comparator from a λ?
Liutauras Vilda wrote:Anyway, Puspender Tanwar, have a cow for great post. It seems you're going to be busy now with cattle breeding
Thanks a lot Vilda, It feels amazing on getting the first cow. I was waiting for this from the first answer I gave on coderanch.![]()
Puspender Tanwar wrote:Thanks Campbell . I was also thinking that using the argument depends on the algo which is being used, but was not sure. Thanks for making me sure
The reason I think of for error prone could be, suppose we want to sort on price basis, where price is any double value. In that case it would not work as expected. This is the only reason which came to my mind. Could you please explain any other reason ?
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Puspender Tanwar wrote:(...)If your Student are having nature of Comparable i.e Students implements Comparable, than Student can only be sorted either on rollNo or age or name. You cannot sort based on all fields.
There are three kinds of actuaries: those who can count, and those who can't.
Liutauras Vilda wrote:How about negative values? -4 - -5
There are three kinds of actuaries: those who can count, and those who can't.
Agh, my bad, right, +1 it is what we expect. -4 - 5 that should mess up things.Piet Souris wrote:
Liutauras Vilda wrote:How about negative values? -4 - -5
? Gives 1, as it should (but to whom was this question aimed?)
praveen kumaar wrote:reason is what if one of the argument is a "negative" value in that case you will get wrong result and also may be overflow occurs because difference can go beyond the integer maximum value - ( 2^31-1).
( 2^31-1 )-( -2^31 )--->overflow.
or
-2^31-( 2^31-1 )---->again overflow.
Anyway! congrats for your first cow puspen
Piet Souris wrote:
Puspender Tanwar wrote:(...)If your Student are having nature of Comparable i.e Students implements Comparable, than Student can only be sorted either on rollNo or age or name. You cannot sort based on all fields.
hi Puspender,
I'm not sure I uderstand this correctly. Can you elaborate a little?
There are three kinds of actuaries: those who can count, and those who can't.
We all know that after 55BC,kiran madhan wrote:. . . we have two methods in Comparator<T> interface.... . . .
Well, this is the peculiar Java® method of counting. Start by getting a Java7 version of Comparator and you can see clearly that it hasSellars and Yeatman, 1066 and All That, chapter 1 wrote:[Julius Caesar was] compelled to invade Britain again the following year (54 BC, not 56, owing to the peculiar Roman method of counting).
Why? I can't see anything wrong with that formula. It will return something negative to show that −4 < 5.Liutauras Vilda wrote:. . . -4 - 5 that should mess up things.
As PS has hinted, that bit about only one field is incorrect.Puspender Tanwar wrote:. . . IF Student class implements Comparable interface, then we have only one filed on which we can sort our List at a time. . . .
I might take some rest today, it's been hard day for me, I totally wrote nonsense in my 2 latest posts.Campbell Ritchie wrote:
Why? I can't see anything wrong with that formula. It will return something negative to show that −4 < 5.Liutauras Vilda wrote:. . . -4 - 5 that should mess up things.
Taking some beer will probably do you even more good.Liutauras Vilda wrote:. . . I might take some rest today, . . .
Piet Souris wrote:Yes, thank you for this very quick elaboration, good for your second cow!
But, in your first reply, in the compareTo method: if both Students have the same rollno, can't you next compareTo the names or so?
Welcome to the RanchShekhar Ray wrote:. . . In such scenario, the Comparator takes precedence over Comparable.
Reference:
TopJavaTutorial
I also think you are mistaken about sorting information not available in the object. If you are sorting objects, you must sort them on information inside the object. If you read the Java™ Tutorials section (I am 99% sure I have already posted that link elsewhere), you will find out about sorting. Some objects have a natural ordering; if you are given the Integers 5, 4, 3, 1, 2, it will be pretty obvious to everybody that they order as 1, 2, 3, 4, 5.The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.
kiran madhan wrote:my question is when to use Comparable and what are the problems are ....and when to use Comparator.?
Shekhar Ray wrote:Comparable interface is used when sorting order can be added inside the same class and the objects can be sorted according to natural order of fields.
Comparable interface is used when sorting information isn’t available inside the object itself or a custom sorting logic needs to be implemented.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |