Problem sorting a Comparable class

Hi folks,

I am trying to sort a Comparable class called Tree by its height h. I tried using the Arrays.sort () function, but it does not compile and the error message is: 'Cannot find anything named "Arrays"'. I tried using collections.sort (), but get the equivalent error message. The code is as follows:

class Tree implements Comparable {
  int    h;  

  Tree (int tempH) {
  h    = tempH;   
  }
  // Make Trees comparable by height
  int compareTo(Object o){
    Tree e = (Tree) o;
    return h-e.h;
  }

void draw (){
  nums = 2000;
  Tree [] trees = new Tree [nums];

  // Loop over the number of individuals 
  //Some code that puts heights into the 2000 trees.

  trees = Arrays.sort(trees);
}

I use data from a file that is read in for a particular year and gives all trees its height. The bit of code that allocates height to trees definitively works, but is quite long so I omitted it.

I have never used Comparable classes before and I am still not sure I have understood them, so any pointer would be very much appreciated.

Cheers, finisTTR

Answers

  • Answer ✓

    add to the top of file

    import java.util.Arrays;
    

    (newer processing versions don't import as much as the older versions so you have to so it yourself)

  • That fixed it! Thanks, koogs!

  • edited September 2014

    Addendum: return h - (Tree) o.h; only works if field h is NEVER negative. Breaks Comparable's contract!
    It's not your case here, since h is Tree's height. But for other folks doing the same, but negative is possible:
    return Integer.signum(h - (Tree) o.h);

    P.S.: If you just use sort() in 1 or 2 places only, you can go w/ explicit: java.util.Arrays.sort(trees);
    rather than import java.util.Arrays + Arrays.sort(trees); :-j

    Extra: We can specify the type to be used in Comparable: class Tree implements Comparable<Tree> {}
    This way we don't need to use Object as parameter type, but go straight to Tree: :D

    @ Override int compareTo(Tree t) {
      return h - t.h;
    }
    
  • edited September 2014

    Ah! A Tree example from an old thread: =:)
    http://forum.processing.org/two/discussion/1645/trees

    "SortedForest.pde":

    /** 
     * Sorted Forest (v3.38)
     * by  AlexChavez (2013/Nov)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/1645/trees
     *
     * forum.processing.org/two/discussion/7187/
     * problem-sorting-a-comparable-class
     */
    
    static final int ROWS = 3, COLS = 10;
    final Tree[][] trees = new Tree[ROWS][COLS];
    
    void setup() {
      size(800, 600, JAVA2D);
    
      frameRate(20);
      noLoop();
      smooth(4);
    
      rectMode(CENTER);
      ellipseMode(CENTER);
    
      Tree.leafW = (short) (width/11);
      Tree.leafH = (short) (height/11);
    
      initForest(true);
      sortForest();
    }
    
    void draw() {
      background(56, 222, 229);
    
      for (Tree[] tcols: trees)  for (Tree t: tcols) {
        t.drawTrunk();
        t.drawTreeTop();
        print(t.h + " - ");
      }
    
      println();
    }
    
    void mousePressed() {
      initForest(false);
      sortForest();
      redraw();
    }
    
    void keyPressed() {
      mousePressed();
    }
    
    void initForest(final boolean toInstantiate) {
      for (Tree[] tcols: trees)  for (int col = 0; col != COLS; col++)
        if (toInstantiate)  tcols[col] = new Tree();
        else                tcols[col].respawnTree();
    
      if (toInstantiate)  setForestPosY();
    }
    
    void sortForest() {
      for (Tree[] tcols: trees)  java.util.Arrays.sort(tcols);
      setForestPosX();
    }
    
    void setForestPosX() {
      final int trunkW = width/16, gapW = width/26;
      final int offset = width/83;
    
      for (Tree[] tcols: trees)  for (int col = 0; col != COLS;
        tcols[col].x = (short) (col++*trunkW + col*gapW + offset));
    }
    
    void setForestPosY() {
      final int trunkH = height/12, gapH = height/26;
    
      for (int row = 0; row != ROWS; ++row)  for (Tree t: trees[row])
        t.y = (short) (row*trunkH + (row + 1)*gapH);
    }
    


    "Tree.pde":

    static abstract class TreeSharedVars {
      static final color TRUNK_C = #6C452A;
    
      static final color[] COLORS = {
        #144D0C, #550106, #FA6305
      };
    
      static short leafW, leafH;
    }
    
    final class Tree extends TreeSharedVars implements Comparable<Tree> {
      short x, y, w, h;
      color ink;
    
      Tree() {
        respawnTree();
      }
    
      void respawnTree() {
        w = (short) random(width/150, width/19);
        h = (short) random(height/13, height*2/9);
    
        ink = COLORS[(int) random(COLORS.length)];
      }
    
      void drawTrunk() {
        fill(TRUNK_C);
        rect(x, y + height/2, w, h, 3);
      }
    
      void drawTreeTop() {
        fill(ink);
        ellipse(x, y - height/13 + height/2, leafW, leafH);
      }
    
      @ Override int compareTo(final Tree t) {
        //return (int) Math.signum(h - t.h);
        //return Integer.signum(h - t.h);
        return h - t.h;
      }
    
      @ Override String toString() {
        return "(x, y, w, h) -> [ " + x + ", " + y + ", "
          + w + ", " + h + " ]\t\tColor -> #" + hex(ink, 6);
      }
    }
    

Sign In or Register to comment.