On Oct 30, 10:00  䮮
> Josef 'Jupp' Schugt wrote:
> > def datecmp(a, b, reasonable = true)
> >    
> >   ۲ ۲ݩ
> >    ۲ ۲ݩ > >  
> > cmp = (arra[0] <=> arrb[0])) != 0 ? cmp : (arra[1] <=> arrb[1])
> >  > > cmp = (arra[1] <=> arrb[1])) != 0 ? cmp : (arra[0] <=> arrb[0])
> >  
> > end
>
> > date = ['12/01/2007', '11/02/2007', '11/11/2005']
> > puts "reasonable date format"
> > puts date.sort { |a, b| datecmp(a, b) }
> > puts "\nno reasonable date format"
> > puts date.sort { |a, b| datecmp(a, b, false) }
>
> I think you got "reasonable" and "unreasonable" reversed (unless you are
> American :-)
>
> Here is a shorter solution:
>
> MapR = lambda { |x| x.split('/').values_at(2,1,0) }
> MapU = lambda { |x| x.split('/').values_at(2,0,1) }
>
> date = ['12/01/2007', '11/02/2007', '11/11/2005']
> puts "reasonable date format"
> puts date.sort_by(&MapR)
> puts "\nno reasonable date format"
> puts date.sort_by(&MapU)


MAP_R = proc{|x| x.split('/').reverse }
MAP_U = proc{|x| y=x.split('/'); [y.pop,y] }

dates = %w(12/01/2007 11/02/2007 11/11/2005)
puts "reasonable date format"
puts dates.sort_by(&MAP_R)
puts "unreasonable date format"
puts dates.sort_by(&MAP_U)