using sort() on user made classes

edited September 2015 in How To...

I have some fish objects, in a 1D array that i need to sort, so that the bigger fish, will be first in the array. I know that i can use sort() to sort numbers, strings and characters.

From c++ i know the function std::sort(), which can sort any array of objects that has a < operator, even user defined calsses.

So to sort my fish in c++, i would only need to add a < operator function, like this:

class fish
{
int size;
...
bool operator<(const fish& that) const{return size<that.size;}
...
}

and then i would be able to use std::sort() on an array, of fish.

But i do still want to make this project in processing.

And in processing, there is no way to overload operators, so what am i then suposed to do, if i want to use sort on a class i have made.

Answers

  • This is a way to do it.

    import java.util.Collections;
    
    ArrayList<Fish> fishies = new ArrayList<Fish>();
    
    void setup()
    {
      size(600,400);
      for(int i = 0; i < 20; i++)
      {
        fishies.add(new Fish());
      }
      noStroke();
    }
    
    void draw()
    {
      background(20,70,128);
      for(Fish fish : fishies) fish.display();
    }
    
    void mouseClicked()
    {
      //sort the fish
      Collections.sort(fishies);
    
      //update their positions so it reflects the sorting
      int i = 0;
      for(Fish fish : fishies)
      {
        fish.position.set(i++*width/fishies.size(), height*0.5);
      }
    }
    
    
    //implements means you implement an interface (Comparable)
    //This means you need to implement the methods of that interface, in this case compareTo();
    //In this way Java can treat the Fish class as something that is Comparable, which is required for the Collections.sort() method.
    class Fish implements Comparable<Fish>
    {
      int size;
      PVector position;
      color colour;
    
      Fish()
      {
        size = (int) random(1, 30);
        position = new PVector(random(width), random(height));
        colour = color(random(255), random(255), random(255));
      }
    
      void display()
      {
        fill(colour);
        ellipse(position.x, position.y, size, size);
        triangle(position.x+size*0.5, position.y, position.x+0.75*size, position.y+size*0.5, position.x+0.75*size, position.y-size*0.5);
      }
    
      //As promised by having the class implement Comparable, we need to implement the compareTo method here. 
      //The description of the compareTo method says that it needs to return an integer 
      //which is < 0 if this object is less than the argument, == 0 if the object is equal to the argument and > 0 if the object is greater than the argument.
      int compareTo(Fish fish)
      {
        return this.size-fish.size;
      }
    }
    

    This is not Processing-specific, but something from Java. So if you ever get stuck at a similar problem, you can search how to do it in Java instead.

    Here's the documentation of the Comparable interface: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html Relevant discussion: http://stackoverflow.com/questions/18441846/how-to-sort-an-arraylist-in-java

  • java.util.Collections.sort() is for non-array containers.
    java.util.Arrays.sort() is what you want for regular arrays.

    Some old examples from the forum: O:-)
    http://forum.Processing.org/two/discussion/7001/how-to-parse-time
    http://forum.Processing.org/two/discussion/8973/sorting-list-of-objects-by-object-variable

Sign In or Register to comment.