### Re^3: New Alphabet Sort Order

by furry_marmot (Pilgrim)
 on Apr 04, 2011 at 22:55 UTC ( #897404=note: print w/replies, xml ) Need Help??

in reply to Re^2: New Alphabet Sort Order
in thread New Alphabet Sort Order

Polyglot,

I think BrowserUK's solution is the direction you'll end up going. The ST and GRT he referred to are the Schwartzian Transform and Guttman Rosler Transform, respectively. You only need the first.

The link explains it, but basically you transform a list of things you want to sort into a list of two-element arrays. The first element is the key, set up so you can easily sort it. The second is the original element, untouched. You can do it in separate steps, but the transform is more efficient if you have a lot of elements. The key thing you need is a function that can create a function that will create a key that you can sort on.

Here's an example, sorting movie names, done separately first.
```use Lingua::EN::Numbers qw(num2en);
sub make_key {
\$_ = shift;
s/^(?:The|An|A) // || s/^[^A-Z_]+(\d+)/num2en;
return \$_;
}

my @movies = (
'(500) Days of Summer',            # F for Five hundred
'The Music Man',                   # M for Music
'The Good, the Bad, and the Ugly'  # G for Good
);

my @tmp = ();
for (@movies) {
push @tmp [ make_key(\$_), \$_ ]     # 2-element anonymous array
}

@tmp = sort { \$a->[0] cmp \$b->[0] } @tmp; # sort on first elements

@movies = map { \$_->[1] } @tmp;      # pull off second element from ea
+ch
# anonymous array
print "\$_\n" for @movies;

__END__
Prints:

(500) Days of Summer
The Good, the Bad, and the Ugly
The Music Man
Now, MUCH less complicated:
```use Lingua::EN::Numbers qw(num2en);
sub make_key {
\$_ = shift;
s/^(?:The|An|A) // || s/^[^A-Z_]+(\d+)/num2en;
return \$_;
}

my @movies = (
'(500) Days of Summer',            # F for Five hundred
'The Music Man',                   # M for Music
'The Good, the Bad, and the Ugly'  # G for Good
);

# Here's the transform. Read from the bottom up.
my @movies =                    # original elements replaced with same
+, but sorted
map  { \$_->[1] }              # pull off second element from each
sort { \$a->[0] cmp \$b->[0] }  # sort arrays on first elements
map  { [ make_key(\$_), \$_ ] } # 2-element anonymous array becomes ne
+w \$_
@movies;

print "\$_\n" for @movies;

__END__
Prints:

(500) Days of Summer
The Good, the Bad, and the Ugly
The Music Man
Good luck!

--marmot

UPDATE: Corrected a typo bug in the ST that came from copying the first version.

Create A New User
Node Status?
node history
Node Type: note [id://897404]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-07-26 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (387 votes). Check out past polls.