我在将数组排序为升序时遇到问题,然后从数组中选择一个值放入新数组中。
# Splitting dance scores with "," and putting into arrays.
for dancers in results
a = dancers.split(",")
couplenumber = a[0]
score1 = a[1]
score2 = a[2]
score3 = a[3]
score4 = a[4]
score5 = a[5]
score6 = a[6]
score7 = a[7]
dancescores << Dancer.new(couplenumber, score1, score2, score3, score4, score5, score6, score7)
# Sorts the array into ascending order, and shows the 4 lowest values.
#p dancescores.sort.take(4)
# Getting the m value, from picking the 4th lowest number.
m = a[4]
newtest = [couplenumber, m]
coupleandscore << newtest
coupleandscore
end
puts coupleandscore
现在它给了我新数组中的原始值,这是它应该的。但如果我尝试做
p dancescores.sort.take(4)
我会收到这个错误:
[#<Dancer:0x10604a388 @score7=5, @score3=3, @score6=6, @score2=2, @score5=1, @score1=1, @couplenumber="34", @score4=3>]
examtest.rb:43:in `sort': undefined method `<=>' for #<Dancer:0x10604a388> (NoMethodError)
任何形式的帮助将不胜感激!