Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

implementing Comparator with double instead of int

807580Mar 25 2010 — edited Mar 25 2010
I have a class called Target with a parameter called double currentAzimuth that is returned by .getCurrentAzimuth(). In another class, I have an ArrayList full of these Target objects. I am needing to sort the List by each objects currentAzimuth.

This is what I am trying in my Target class:
	static class AzimuthComparator implements Comparator
	{
		public double compare(Object o1, Object o2)
		{
			Target t1 = (Target)o1;
			Target t2 = (Target)o2;
			return t1.getCurrentAzimuth() - t2.getCurrentAzimuth();
		}
	}
And this in my other class:
	Collections.sort(targetArrayList, new AzimuthComparator());
Eclipse is giving me this error: The return type is incompatible with Comparator.compare(Object, Object)

I looked up the Comparator in the API (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html#compare%28T,%20T%29) and sure enough, it only returns an int.

Am I going about attempting this correctly, and if not, what should I be trying to do. Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 22 2010
Added on Mar 25 2010
14 comments
1,248 views