Comparable and compareTo() error: Cannot invoke compareTo(float) on the primitive type float
in
Programming Questions
•
1 year ago
Hi!
I'm trying to implement a Comparable for the first time and having some (I suspect) is a casting issue.
I've got a simple myRect class that creates a Rect and Polygon2d (via Toxilibs). I've got a List of these myRects that I'm trying to sort first by height and then by width.
Here is the code:
- import toxi.geom.*;
- import toxi.geom.mesh2d.*;
- import toxi.processing.*;
- import toxi.geom.mesh.*;
- import toxi.math.conversion.*;
- import java.util.Comparator;
- List<myRect> rects = new ArrayList<myRect>();
- int nRects=5;
- ToxiclibsSupport gfx;
- void setup() {
- gfx=new ToxiclibsSupport(this);
- size(500, 500);
- // translate(250,250);
- for (int i=0; i<nRects; i++) {
- println(i);
- rects.add( new myRect(100,500) );
- noFill();
- gfx.polygon2D(rects.get(i).polyR);
- }
- }
- public class myRect implements Comparable<myRect> {
- Rect r;
- Polygon2D polyR;
- float w, h, a;
- public myRect(float f1, float f2) {
- r = new Rect( new Vec2D(random(f1, f2), random(f1, f2)), new Vec2D(random(f1, f2), random(f1, f2)) );
- polyR=r.toPolygon2D();
- if (polyR.isClockwise()) {
- polyR.reverseOrientation();
- }
- // w=r.width;
- // h=r.height;
- //
- }
- public int compareTo(myRect inr) {
- int lastCmp = this.r.height.compareTo(inr.r.height);
- return (lastCmp != 0 ? lastCmp :
- r.width.compareTo(inr.r.width));
- }
- }
I've cribbed this code from the Java Tutorial, and according to the Toxilibs Java doc for the Rect class, the height field is a float.Cannot invoke compareTo(Float) on the primitive type float
I've tried a few things but can't seem to get pas this error.
Like I said this is my first time implementing and using
Comparable and compareTo so I'm probably making a simple mistake. And I know there is a simpler way to directly compare the width and height, however, I really want to learn how to use comapreTo properly.
Can anyone point out what I'm doing wrong?
Thanks in advance!
1