• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparators

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.. so I think I understand Comparators and the reason why Comparable and Comparator are different. It has to do with where the code is located. My question it this. Since I'm using an interfece and implementing Comparator, can't I add my own functions and fields to the object that implements Comparator. See I need to be able to sort 1 record set several different ways, and I'd like to set a flag within my class that impliments Comparator, however when I try to access my extra methods, Visual Age for Java doesn't see it. What am I missing. Is this possible.. I really don't want to create 8 different classes for 8 different types of sorts. ( 4 columsn that cna be sorted ASC or DESC)
Any suggestions?
Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
Jianfei Yang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am not sure about your problem, I think your problem is the reference to your object. If you reference is define as comparator, you certainly can not see the attributes and your methods.
 
shashank bapat
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dale,
JY's reasoning seems right .. if not possible problem can be scope of methods and fields.
Suggestions:
1.> Unless required use anonymos inner classes. This will reduce lot of pain.
2.> Comparator is in a way logic used as data. If u think that at two places u have ditto same logic of compare() then u should be creating only one object and will no longer be able to keep the class anonymos ... it still can be inner.
3.> If u think that number of classes can be further reduced by capturing some common control attributes for ur comparator logic Go ahead ... its good design.
Typical case as u pointed out will be
if(ASCENDING) return a-b;
else return b-a;
It seems that u have understood the concept and u r on the right track I hope it helps in building confidance in ur own design ... but I can not really solve the real problem unless u paste some code here
Thanks
-Shashank
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.. so I have it going so far.. but my new issue is this... when I call the compare(String, String) method within the compare(Object, Object) method, it calls itself. I know this seems elementery however with the compareTo method in the Comparable interface, I compared the 2 strings and returns a -1, 0 or 1 respectively. (it did NOT call itself) Do I have to write my own method to compare Strings as well? If so, how might I go abotu this? Here's my code.

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh.. never mind.. .I got it. I actually remembered that the compareTo method exists in every String object and thus returns the comparison accordingly. So I just ended up using the .compareTo() method that exists in that. Here's my code. (and it works great) Oh.. I also had a flaw in my logic in the above code for the return code. It shouldn't be 0, it should have been the compareTo result.
 
shashank bapat
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks fine to me other than overuse of else and u can call compareTo() directly on Strings.

Thanks
Shashank
------------------
-Shashank
MS(CS),BS(CS),SCJP2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic