wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Sorting Object using ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting Object using ArrayList" Watch "Sorting Object using ArrayList" New topic
Author

Sorting Object using ArrayList

David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18




Im trying to use the compareTo() method to get the largest object in the array. The output I get is this:
Position of max area is...3
[Circle Color: null Filled: false 1.0, Circle Color: red Filled: true 1.0, Rectangle Color: null Filled: false 20.0 10.0, Rectangle Color: null Filled: false 5.0 1.0]
[Circle Color: null Filled: false 1.0, Rectangle Color: null Filled: false 20.0 10.0, Rectangle Color: null Filled: false 5.0 1.0, Circle Color: red Filled: true 1.0]

I now am able to get the right max are value, but I seem to be sorting it wrong

Thanks for the help in advance.


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

David Laverdy wrote:Im trying to use the compareTo() method to get the largest object in the array...

Well, I suspect that's your problem; unless ALL your GeometricObject classes are Comparable by area (and would that be "max" area?).

Have you considered a Comparator (java.lang.Comparator) instead?

Winston

PS: I've also broken up your LongLines (←click). Please try to avoid them.

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
This is for a homework assignment and we specifically need to use compareTo(). All of the objects have a area, I can get it to return the one with the largest area. Where is it failing when the sort method works down the array?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

David Laverdy wrote:This is for a homework assignment and we specifically need to use compareTo(). All of the objects have a area, I can get it to return the one with the largest area. Where is it failing when the sort method works down the array?

OK, well you appear to be trying to implement a recursive version of a bubble sort - why, I'm not quite sure - but simply put, it won't work.

I suspect you may have intended a selection sort, but that won't work without an iteration through ALL "remaining" elements.

Also, it's a horrible use of recursion (unless, that was also a requirement).

Winston
David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
Every time he has a sort is only through recursion.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Obviously we don't know the details of the assignment. But you do realise that to find the max value you don't need to sort the list? You just need to traverse it once comparing values.

There's even a built-in method to do this for you, but I'll assume you're not allowed to use that.

(I was about to say that you should be checking for compareTo(...) < 0, not == -1, but it looks like that's been fixednow).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
David Laverdy wrote:Every time he has a sort is only through recursion.
Every time who?
I have only seen merge sort and quicksort done recursively. Bubble and selection sort are usually done with loops.
David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
My teacher
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

David Laverdy wrote:My teacher

Right, so we now have two requirements that you didn't explain at the start:
1. You must use the class's natural order.
2. You must use recursion for your sort method.

Unless your teacher also requires that the sort is stable, I suggest you look at the Quicksort algorithm.

Winston
David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
Sorry for not mentioning it still new here. I'll read through that link and work with that
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

David Laverdy wrote:Sorry for not mentioning it still new here...

No probs. But it's worth remembering that when you ask questions, you must TellTheDetails (←click).

Winston
 
 
subject: Sorting Object using ArrayList
 
Similar Threads
ArrayList
JApplet
fileReader + GUI
Sorting the String Saved in ArrayList
Trouble printing objects contents through ArrayList