Ruby Array没有排序

ray*_*ray 2 ruby arrays sorting

我正在尝试对数组进行排序,但它总是返回相同的原始输入.你知道为什么吗?

puts "Please tell me the array(separated by space, finish by enter twice):"
x = []
input = ' '
while input != ''
  input = gets.chomp
  x.push input
end

puts x.sort
Run Code Online (Sandbox Code Playgroud)

tad*_*man 6

您需要split输入和concat单个数组:

x = [ ]

loop do
  input = gets.chomp

  break if (input.empty?)

  x += input.split
end

puts x.sort
Run Code Online (Sandbox Code Playgroud)