Hi Monks,
I am trying to sort an array using the Schwartzian transform on the first element of
num1_num2
use strict;
my @unsorted = (
'864278_864377',
'864518_864703',
'851171_851270',
'855398_855579',
'856258_856357',
'861015_861139',
'866387_866549',
'791806_792296',
'1088806_1082296',
);
my @sorted = map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [ $_, ( split(/\_/, $_) )[0] ] }
@unsorted;
print join ("\n",@sorted);
I am expecting the sorted array to be in this order:
791806_792296
851171_851270
855398_855579
856258_856357
861015_861139
864278_864377
864518_864703
1088806_1082296
Instead I am getting this:
1088806_1082296
791806_792296
851171_851270
855398_855579
856258_856357
861015_861139
864278_864377
864518_864703
866387_866549
I just can't figure out how to fix this!
Thanks much for your input!
UPDATE: I fixed it by using
<=> numeric sort!
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, details, 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, summary, 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.