From: MonkeeSage Date: 2007-12-26T18:55:00+09:00 Subject: Re: Sorting Titles in Ruby On Dec 24, 12:01 pm, Thomas Wieczorek wrote: > On Dec 24, 2007 6:11 PM, Rick DeNatale 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 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