aspose file tools*
The moose likes Java in General and the fly likes Comparators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparators" Watch "Comparators" New topic
Author

Comparators

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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)


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Jianfei Yang
Greenhorn

Joined: Aug 23, 2001
Posts: 4
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

Joined: Jul 11, 2001
Posts: 69
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

-Shashank<BR>MS(CS),BS(CS),SCJP2
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
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

Joined: Nov 02, 2000
Posts: 515
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

Joined: Jul 11, 2001
Posts: 69
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparators