File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Object using ArrayList

 
David Laverdy
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Pie
Posts: 9492
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Laverdy
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9492
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time he has a sort is only through recursion.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My teacher
 
Winston Gutkowski
Bartender
Pie
Posts: 9492
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for not mentioning it still new here. I'll read through that link and work with that
 
Winston Gutkowski
Bartender
Pie
Posts: 9492
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic