• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Object using ArrayList

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every time he has a sort is only through recursion.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My teacher
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic