We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Problem with sort() on array of objects.
Page Index Toggle Pages: 1
Problem with sort() on array of objects. (Read 348 times)
Problem with sort() on array of objects.
Feb 5th, 2009, 1:52am
 
I have an array of objects of type Entity, each of which has a "fitness" member that's a float:

Entity[] population;
...
class Entity implements Comparable {
 float fitness;
...
 public int compareTo(Object o)
 {
   Entity e = (Entity)o;
   int result = 0;
   
   if(fitness > e.fitness)
     result = 1;
   else if(e.fitness < fitness)
     result = -1;
   else
     result = 0;
   return result;
 }
}

Elsewhere, I try to sort the "population" array:

population = sort(population); // have to assign because sort doesn't operate in-place.

When I try to run it, I get this:

The method sort(byte[]) in the type PApplet is not applicable for the arguments (ImageEvolver.Entity[])

I'm at a loss, here.  I thought sort() was smart enough to sort arrays of objects.  What's the trick?
Re: Problem with sort() on array of objects.
Reply #1 - Feb 5th, 2009, 9:08am
 
the sort() docu says:
Parameters:
dataArray String[], int[], or float[]

so no objects.

Try Arrays.sort(population);

Thats from the Java-API.
Re: Problem with sort() on array of objects.
Reply #2 - Feb 5th, 2009, 7:29pm
 
Thanks.

Processing is also my first exposure to Java, so there's a lot of newbie stuff I'm learning on that front too.
Page Index Toggle Pages: 1