On Dec 26, 3:51 am, MonkeeSage <MonkeeS... / gmail.com> wrote:
> On Dec 24, 12:01 pm, Thomas Wieczorek <wieczo... / googlemail.com>
> wrote:
>
> > On Dec 24, 2007 6:11 PM, Rick DeNatale <rick.denat... / gmail.com> wrote:
>
> > > In other words, leading articles should be moved to the end, and
> > > numbers should be spelled out before sorting.
>
> > You can use the Linguistics
> > gem(http://www.deveiate.org/projects/Linguistics/) to convert numbers
> > to words. I never used it, so you need probably a simple regex to
> > extract the numbers.
> > Leading articles can be easily removed with a regex.
>
> Simple implementation of that...
>
> # Glenn Parker's solution to Ruby Quiz #25 [1]
> require "num2eng"
>
> class String
>   def title_case
>     # from somewhere...can't recall
>     gsub(/\b\w/) { $&.upcase }
>   end
>   def canonical_form
>     nums = scan(/[\d,_]+/)
>     unless nums.empty?
>       nums.each { | num |
>         sub = num.gsub(",","").to_i.to_english.title_case
>         gsub!(num, sub)
>       }
>     end

Err...that's silly. I meant...

    scan(/[\d,_]+/).each { | num |
      sub = num.gsub(",","").to_i.to_english.title_case
      gsub!(num, sub)
    }

...too much eggnog this evening, heh. ;)


>     gsub(/\A(An?|The)(.*)/, '\2, \1').strip
>   end
> end
>
> titles = [
>   "20,000 Leagues Under the Sea",
>   "A Fine Madness",
>   "Fanny",
>   "Marooned",
>   "The Man Who Came to Dinner",
>   "5 Days and 6 Nights of Fictitious Cinema"
> ]
>
> puts titles.map { | title |
>   title.canonical_form
>
> }.sort
>
> # =>
> Fanny
> Fine Madness, A
> Five Days and Six Nights of Fictitious Cinema
> Man Who Came to Dinner, The
> Marooned
> Twenty Thousand Leagues Under the Sea
>
> Ps. Corner cases like "20, 000 Leagues ..." will break, but it's a
> start. :)
>
> [1]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/135449
>
> Regards,
> Jordan