LPr*_*Prc 2 ruby arrays sorting ruby-1.8
我想通过跳过第一个数组的第一个字符串对字符串数组的数组进行排序,但我只是不知道如何使用内置sort方法进行排序。我可以复制没有第一个元素的整个数组,然后对结果数组进行排序,但是没有更优雅的方法来做到这一点吗?
ar = [["zzzz", "skip", "this"], ["EFP3","eins","eins"], ["EFP10","zwei","zwei"], ["EFP1","drei","drei"]]
ar.sort!{ |a,b|
if a == ar.first # why doesn't
next # this
end # work ?
# compare length, otherwise it would be e.g. 10 < 3
if a[0].length == b[0].length
a[0] <=> b[0]
else
a[0].length <=> b[0].length
end
}
Run Code Online (Sandbox Code Playgroud)
我想要这样的结果:
["zzzz", "skip", "this"], ["EFP1","drei","drei"], ["EFP3","eins","eins"], ["EFP10","zwei","zwei"]
Run Code Online (Sandbox Code Playgroud)
排序方式 "EFP#"
编辑:如果重要的话,我使用的是 Ruby 1.8。