Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!elroy.jpl.nasa.gov!swrinde!ucsd!ucbvax!agate!usenet From: raymond@math.berkeley.edu (Raymond Chen) Newsgroups: comp.lang.perl Subject: Re: Help with split, replace and number formats. Message-ID: <1991Jan30.195436.8645@agate.berkeley.edu> Date: 30 Jan 91 19:54:36 GMT References: <1991Jan29.030541.46@eagle.inesc.pt> <1991Jan29.171228.17738@convex.com> <1991Jan30.181924.47@eagle.inesc.pt> Sender: usenet@agate.berkeley.edu (USENET Administrator) Reply-To: raymond@math.berkeley.edu (Raymond Chen) Organization: U.C. Berkeley Lines: 33 In-Reply-To: jmc@eagle.inesc.pt (Miguel Casteleiro) In article <1991Jan30.181924.47@eagle.inesc.pt>, jmc@eagle (Miguel Casteleiro) writes: >[T]he sorting order will be (at least for the portuguese): > >a A B b c d e f ... > >If there is an easy way to do this, please let me know. # This is a standard trick. # You only need to do this part once. $portuguese_order = "aABbcdef"; $ascii_order = pack("c" . length($portuguese_order), 1 .. length($portuguese_order)); eval 'sub port2sort { foreach (@_) { tr/'.$portuguese_order.'/'.$ascii_order.'/; } }'; eval 'sub sort2port { foreach (@_) { tr/'.$ascii_order.'/'.$portuguese_order.'/; } }'; # and here's how you use it: @words = ("a", "A", "B", "c", "b"); &port2sort(@words); # convert to intermediate format @sorted_words = sort @words; # sort the intermediate format &sort2port(@sorted_words); # convert back print join(":", @sorted_words); # Observe that a similar trick can be used to perform other types of sorting; # for example, if you want digits to sort *after* letters, or if you want # the letter "p" to alphabetize before the letter "h", like this: for(sort("herl ","Just ","packer,","anotper ")){y/Jahp/Japh/;print;}