Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Dictionary-style sort a la Tcl?

by blakem (Monsignor)
on Apr 18, 2002 at 10:38 UTC ( #160167=note: print w/ replies, xml ) Need Help??


in reply to Dictionary-style sort a la Tcl?

Here is a quick late-night attempt.... It assumes the words only consist of [A-Za-z] and relatively small integers. Not perfect, but it does work on your sample dataset. Oh, it also sorts 'aa' before 'a1' which is probably not correct, but you didn't mention it in the requirements.

#!/usr/bin/perl -wT use strict; { # Create an interleaved 'AaBbCc' mapping my %map; $map{$_} = 2*(ord($_)-ord('A')) for 'A'..'Z'; $map{$_} = 2*(ord($_)-ord('a'))+1 for 'a'..'z'; # use interleaved map to munge word into a string that will sort "co +rrectly" sub dictize { my $word = shift; $word =~ s/(\d+|[A-Za-z])/chr($map{$1}||60+$1)/ge; return $word; } } my @words = qw(bigBoy bigbang bigboy x10y x9y x11y); # Sort based on lowercased munged words, # use the non-lowercased version as a tiebreaker my @sorted = map {$_->[0]} sort {$a->[1] cmp $b->[1] or $a->[2] cmp $b->[2]} map {[$_,dictize(lc),dictize($_)]} @words; print "$_\n" for @sorted; __END__ bigbang bigBoy bigboy x9y x10y x11y

-Blake


Comment on Re: Dictionary-style sort a la Tcl?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://160167]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-12-27 22:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls