Problems? Is your data what you think it is? PerlMonks

### Comment on

 Need Help??

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
[download]```
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
[download]```
Good luck!

--marmot

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

In reply to Re^3: New Alphabet Sort Order by furry_marmot
in thread New Alphabet Sort Order by Polyglot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.
• Log In?
 Username: Password:

What's my password?
Create A New User
Chatterbox?
and the sunlight beams...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-05-23 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (172 votes). Check out past polls.

Notices?