On Jun 5, 2009, at 3:56 PM, 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?
>
> Also having problems with a Regexp, but I can't find the appropriate  
> forum to cry in.
>
> Cheers,
>
> Bob Schaaf


Why make the arrays equal length with nils?

%w[ 1.3 1.1 1.2.1 1.2 1 ].sort_by{|outline| outline.split('.').map{|n| 
n.to_i} }
=> ["1", "1.1", "1.2", "1.2.1", "1.3"]

We can handle Regexp questions here, too... but you have to toss in a  
bit of ruby to keep it fair. ;-)

-Rob

Rob Biedenharn		http://agileconsultingllc.com
Rob / AgileConsultingLLC.com