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 › Sort objects in ArrayList by classmethod result
Page Index Toggle Pages: 1
Sort objects in ArrayList by classmethod result (Read 601 times)
Sort objects in ArrayList by classmethod result
Feb 6th, 2009, 3:34pm
 
Hey people,

The situation: I have an ArrayList of 4 objects (of Class Line). Within this Class I have a method to determine the angle of this line (Line.getAngle).
Now I want to sort the 4 objects (Lines) from highest angle to lowest angle, and use this order to draw a polygon, so I need the ArrayList again in the main application.
How can I do this? Do I need to use the Comparable method? If so, how does this work, because I never used it before and after some short research I still do not see how I can sort from higher number to lower number...

Thanks in advance!
Re: Sort objects in ArrayList by classmethod resul
Reply #1 - Feb 6th, 2009, 5:18pm
 
The sort array of objects by String inside variable thread discuss the topic at length. The last message shows how to invert the comparison...
Re: Sort objects in ArrayList by classmethod resul
Reply #2 - Feb 6th, 2009, 5:39pm
 
ok, sorry for starting this thread. I just kind of hoped there would be an easier way than the technique in that thread...
Thanks for pointing me to the right thread though
Re: Sort objects in ArrayList by classmethod resul
Reply #3 - Feb 6th, 2009, 9:01pm
 
No problem.
And I fear there is no "simple" way to indicate which field(s) to use when comparing two arbitrary objects.
After all, if you define a Line object, you can sort by x, y of starting or ending point, by color, by angle as you want, by length, by thickness, and so on. Or even by a combination of some of these parameters.
The Comparator/Comparable way might seem complex, but once you understood how to use it, it isn't so much intimidating, and it is very flexible.
Page Index Toggle Pages: 1