From: Brian Candler Date: 2009-06-06T05:05:07+09:00 Subject: Re: Making nil comparable. Robert Schaaf wrote: > I have a array of foo objects, each of which contains an array bar > values. I'd like to sort the foos, ordered by the bars. The problem > is that the bars are of unequal length. > > The most apt example is the numbering in an outline: 1, 1.1, 1.2, > 1.2.1, 1.3 ...etc. > > In practical terms, comparing 1.1 to 1.2.1 would be [1, 1, nil] <=> > [1, 2, 1]. Array#sort_by barfs on the nil. Is there some nice way to > let nil always sort low? irb(main):002:0> [1,1] <=> [1,2,1] => -1 So would sort_by { |x| x.bars.compact } be sufficient? -- Posted via http://www.ruby-forum.com/.