# register and sort alphabetical order ?

kelvin cheung
Ranch Hand
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
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.

kelvin cheung
Ranch Hand
Posts: 120
hi Stan.
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
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
Posts: 120
thanks David Weitzman

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Thanls David - I didn't answer the question atoll did I!

kelvin cheung
Ranch Hand
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?