This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting an array

 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an array of an array and I am getting a bit confused sorting it.



It has been populated e.g



... I think thats how it stored anyway?

How do i sort my array according to 'aValue'

So it would arrange may array as follows:



Ive only included snippets of my code, if I need to provide more, let me know.

Thanks
 
Paul Clapham
Sheriff
Posts: 21149
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way: make your class implement Comparable so it knows how to rank itself:and then to sort it:(If you find that sorts in the wrong direction then interchange 1 and -1 in the compareTo() method.)
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure this isn't legal.

You can't return a boolean instead of an int;

And neither is this:

myArray is the class name. You can call Array.sort() on an array of myArray instances, but not on the class itself.

I think Paul may be getting sleepy.

 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, thanks for your reply.

Is this a good method of doing what I am aiming to do?
If so, I'll play around with it and see how I get on.

If you have any other suggestions, please let me know.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try playing around with it a little, but looking at your original post, I'm a little confused about how your class is structured.
You have the beginning of the class definition as
but then you are saying you are populating it like this
how is that possible. Is myArray a custom class or an array? It can't be both.
[ March 30, 2006: Message edited by: Garrett Rowe ]
 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Gareth, my crap naming conventions are at fault.






I am looking into the Comparable interface, but running into problems.

I have



I understand this should return an int. At the minute its returning a float. I've tried casting the value to an int but its throwing errors.

Is that any clearer?
 
Kashyap Hosdurga
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jackie,
Can you please post the code where you are trying to cast rating to an integer.

I am assuming you are casting it as
return (int)(this.rating - otherRating);
and not
int return (int) this.rating - otherRating;
Please let me know if I am underestimating you.
 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help, think I've sorted it now

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its much clearer to me now. However you still can't populate it the array of grade instances like that. You will have to call a constructor to instantiate a new grade at each element of the array.


As for the compareTo() mehtod you could try this


Another alternative:

Although I'm not sure if thats clearer or not
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to let you be aware of a small gotcha with comparing ints this way

The just of it is although two values may be legal int values, the difference between may overflow.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a further aside, if you are using Java 5, you can use the parameterized Comparable interface.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic