Made couple of changes. (One of those changes is to use '\|' instead of '|'):
my $str = `type unsort.txt`;
$str = join("\n",
map($_->[0],
sort( {$a->[1] <=> $b->[1] }#for alpha order change <=> to
+ cmp
map( [$_, (split('\|', $_))[1] ],#change this number to
+control which field to use, 0 for 1st field
split(/\n/, $str)
)
)
)
);
print $str;
Explaination:
- the split breaks the content of the file, into lines. Now we have a list contains all the lines;
- the inner map creates a list of array ref, which refs an array of two elements, the first one holds the entire line, when the second one contains the sorting key. You can choose which field to be used as the key;
- the sort sorts by the 2nd element, which is the key we choosen;
- the outter map then creates a list contains only the whole lines;
- finally, the join put the line breaks back, and form a string.
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.