1
我正在學習通過Ruby進行學習編程,而且我堅持構建自己的排序方法。學習Ruby - 卡在數組中的字符串比較
我竭力要弄清楚爲什麼我的recursive_sort裏面的比較方法是拋出了一個錯誤
chapter10.rb:120:in `block in recursive_sort': undefined method `<' for ["zebra"]:Array (NoMethodError)
但是,這只是正常工作...
lowest = 'zebra'
if 'cat' < 'zebra'
lowest = 'cat'
end
puts lowest
能幫有人在正確的方向,可以幫助我把我的頭圍繞這個?謝謝!
puts 'Sorting Program with recursion v1.0'
# Keep two more lists around
# One for already-sorted words
# One for still - unsorted words
# Find the smallest word in the unsorted list
# push it into the end of the sorted_array
def sort some_array
recursive_sort some_array, []
end
def recursive_sort unsorted_array, sorted_array
lowest = unsorted_array[0]
unsorted_array.each do |uns|
if uns < lowest
lowest = uns
end
end
puts lowest
end
# Get a list of unsorted words into an array
orig_array = []
word = 'placeholder'
puts 'Enter a list of words to be sorted. Press enter when done.'
while word != ''
word = gets.chomp
orig_array.push [word]
end
orig_array.pop
puts 'This is the output of the built in sort method.'
orig_array.sort.each do |un|
puts un
end
puts 'This is the output of Rick\'s sort method.'
sort orig_array
非常有意義。謝謝你指出! –
@瑞克不客氣!如果答案解決了您的問題,您可以通過單擊左側的綠色複選標記來接受它。 – Doorknob