wood burning stoves 2.0*
The moose likes Java in General and the fly likes Sorting an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting an array" Watch "Sorting an array" New topic
Author

Sorting an array

Jackie Davis
Greenhorn

Joined: Mar 04, 2006
Posts: 23
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jan 17, 2006
Posts: 1296
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.



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Jackie Davis
Greenhorn

Joined: Mar 04, 2006
Posts: 23
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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Mar 04, 2006
Posts: 23
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

Joined: Sep 19, 2005
Posts: 90
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

Joined: Mar 04, 2006
Posts: 23
Thanks for all your help, think I've sorted it now

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Jan 17, 2006
Posts: 1296
As a further aside, if you are using Java 5, you can use the parameterized Comparable interface.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting an array