aspose file tools*
The moose likes Beginning Java and the fly likes register and sort alphabetical order ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "register and sort alphabetical order ?" Watch "register and sort alphabetical order ?" New topic
Author

register and sort alphabetical order ?

kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
hi.
i dont understand this code.
i dont see why it uses the compareTo method.
and why it uses 2 loops.
can someone analyse the code for me ?
thank you
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That's an interesting little algorithm. It's keeping sorted items within a fixed size array. If there's room for one more the first loop finds the slot where the new one belongs. The second loop moves all the larger items one position closer to the end to make an empty slot to put the new item in. Finally, it drops the item into the slot identified by the first loop. This is surely not very efficient, but provides some good exploration of loops and arrays.
Is this homework? I hope I haven't given you too much help - we try not to actually do homework for folks. For a fun exercise, you might try to do this with only the second loop. Think about how you'd make the loop terminate right where you want to put in the new item.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
hi Stan.
thanks for answering me.
this WAS a homework long time ago.
i still dont quite get it.
why does it uses : medlemmer[i].compareTo(navn)<0 ?
why "0" ?
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
To ask your question a little more directly, what does compareTo() do? The answer can be found in the Java API documentation. If you look at the information about compareTo() there it looks like the answer to your question is that a.compareTo(b) < 0 means a.comesBeforeInSortedOrder(b). More importantly, the meta answer is this: learn to love the API documentation and keep a copy under your pillow.
kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
thanks David Weitzman
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Thanls David - I didn't answer the question atoll did I!
kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
ok.
there is 1 more i wonder to fully udnerstand this code.

lets say j = 10.
this says that 10 = 9
and 9 = 8 and so on if its in a FOR LOOP.
i wonder if 10 = 9 ,
then what is 10? i mean 10 has a value/name on an array from before.
its it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: register and sort alphabetical order ?
 
Similar Threads
Sorting with Collator and Collections ??
Comparing values
What is wrong with my generic implementation of sort method ?
accesing elements in a matrix
Find smallest element in arraylist