From: Eric Hodel Date: 2009-06-06T07:16:56+09:00 Subject: Re: Making nil comparable. On Jun 5, 2009, at 12:56, 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? Don't add nil: irb(main):003:0> [[1, 1], [1], [1, 1, 2], [2], [1, 2, 1], [1, 3]].sort => [[1], [1, 1], [1, 1, 2], [1, 2, 1], [1, 3], [2]] If you have nil, use #compact on each Array you're sorting first.