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