So you need to condition the sort test result on quantity and colour.
use strict;
use warnings;
my %cat;
while (<DATA>) {
next if ! /^(\d+)\s+(\d+)$/;
$cat{$1} = $2;
}
my @order = sort {
$cat{$a} && $cat{$b}
? $a <=> $b
: $cat{$a} <=> $cat{$b} || $a <=> $b
} keys %cat;
printf "%-3d %-3s\n", $_, $cat{$_} for @order;
__DATA__
70 3
72 10
74 8
76 11
77 6
90 12
91 8
92 10
93 9
78 0
75 0
73 0
71 0
Prints:
71 0
73 0
75 0
78 0
70 3
72 10
74 8
76 11
77 6
90 12
92 10
93 9
The ternary operator ?: selects compare by colour if neither count is 0, or $cat{$a} <=> $cat{$b} || $a <=> $b which first compares by count then (if both counts are the same) compares by colour.
True laziness is hard work
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.