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 › Sorting arrays of objects
Page Index Toggle Pages: 1
Sorting arrays of objects (Read 746 times)
Sorting arrays of objects
Nov 17th, 2008, 2:26am
 
I have a project that involves one array of thousands of circular objects.  To apply some change to all of them, I use the usual for() loop to cycle through each one, and call a method, etc.

But now I want the ability to apply a change to only a *subset* of the objects, and I want to be able to order them.  For example, I want to identify only the objects at a certain z depth value, and then sort them by largest to smallest.  The end goal is to then feed this list of objects, in sorted order, to a method that will do something with them (e.g. cluster or arrange them by size, left to right).

I thought this would be possible by creating a second array of objects, and then "copying" or "referencing" the subset of objects in that new array.  For example, here is a portion of my code:

Code:

//Create a new object array to store all objects to be clustered
Object[] objectsToCluster = new Object[1];
objectsToCluster[0] = new Object(0, 0);

//Grab all objects with a z depth of zero and throw them in the array
for (int i = 0; i < objects.length; i++) {
if (objects[i].z == 0) {
objectsToCluster = (Object[]) append(objectsToCluster, objects[i]);
}
}


The intent is for the for() loop to cycle through all objects, find the ones where z == 0, and then append them to the objectsToCluster array.

So I guess I have a few questions -- responses to any would be helpful.

1)  Does this approach make sense?

2)  Once I have copied the subset of objects over into the new array, how can I sort them by size?  (Are ArrayLists or HashMaps the answer?  I don't understand either.)

3)  I get an error when trying to pass this new array of objects to a method.  What does "object1.radius cannot be resolved or is not a field" mean?
Re: Sorting arrays of objects
Reply #1 - Nov 17th, 2008, 11:53am
 
Using "Object" as class name isn't a very good idea, because it is a fundamental class in Java... So it might be confusing.

In Display depth question, I give a little bit of code solving a similar problem. I used ArrayList, which isn't that hard to use (if you have issues with them, just ask! Wink), but you can stick to array and use Arrays.sort(yourArray) function to do the sort. The catch is that your class must implement Comparable, as I shown in my snippet (the method must know how to compare objects, which field(s) to use for this).
Re: Sorting arrays of objects
Reply #2 - Nov 17th, 2008, 7:34pm
 
Thanks, PhiLho! I will give this a shot this afternoon.

And sorry for the confusion — in the real sketch, my object class is not called "Object."  I rewrote it here in an attempt to minimize confusion.
Re: Sorting arrays of objects
Reply #3 - Dec 1st, 2008, 3:33am
 
PhiLho, I got it to work perfectly!  Thanks again for your help.  Ended up using a regular Array, which integrated into my existing code more easily than an ArrayList.

As you said, all this took was implementing Comparable, including a compareTo method in the object class, and then this one line took my "objectsToCluster" array and sorts it:

Arrays.sort(objectsToCluster, Collections.reverseOrder());

Wonderful!  This will make so many other projects that are on the horizon so much simpler.
Page Index Toggle Pages: 1