Sebastian Hungerecker
11/19/2007 8:28:00 PM
Joakim Olsson wrote:
> Now a person wrote this to me:
That person was me.
> sorted=[]; words.length.times do sorted<<words.delete(words.min) end;
> sorted
>
> How can I use that in the code?
It returns the sorted array. You can puts it or do whatever you want with it.
> It would be really nice with an example
> which I can read and then understand.
words=gets("").split("\n")
sorted=[];
# At this point sorted is an emtpy array and words is an unsorted array
# of the words that the user entered.
words.length.times do # Do the following as many times
# as there are items in words.
sorted << words.delete(words.min)
# Take the smallest item in words (words.min),
# delete it from words (words.delete) and then
# put it at the end of sorted (sorted <<)
end
puts sorted # Print the sorted array to the screen
HTH,
Sebastian
--
NP: Dark Suns - Virtuos Dilemma
Jabber: sepp2k@jabber.org
ICQ: 205544826