我希望按照第一个字符串排序数组的数组,跳过第一个数组,但是我不知道如何使用内置的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
}
我希望能够得到这样的结果:
["zzzz", "skip", "this"], ["EFP1","drei","drei"], ["EFP3","eins","eins"], ["EFP10","zwei","zwei"]
按"EFP#"
排序
编辑:我正在使用Ruby 1.8,如果有关系的话。