Steve Kpke
Steve Kpke

Reputation: 1

Why is this perl sorting not working as expected?

I first calculate the number of keys in array. When the number of "vectranet" items exceeds 9 the sorting does not work properly - "vectranet" is not first on the sorted list (I sort by hash value), here is my code, please help me I am going crazy :-) This form tells me that my post is mostly code and that I should provide more details, but I cannot write any more text I explained everything and I need to attach this huge array for you to see what my problem is :(

@cprov=(
'vectranet.pl',
'airmax.pl',
'mm.pl',
'aduro.pl',
'lss.net.pl',
'multiplay.pl',
'vectranet.pl',
'vectranet.pl',
'vectranet.pl',
'vectranet.pl',
'vectranet.pl',
'inetia.pl',
'orange.pl',
'orange.pl',
'proneteus.pl',
'zicom.pl',
'zicom.pl',
'ac1-nat13.pl',
'sownet.pl',
't-mobile.pl',
't-mobile.pl',
't-mobile.pl',
'multiplay.pl',
'multiplay.pl',
'cyfrowypolsat.pl',
'plus.pl',
'vectranet.pl',
'vectranet.pl',
'centertel.pl',
'centertel.pl',
'centertel.pl',
'play-internet.pl',
'centertel.pl',
'plus.pl',
'cyfrowypolsat.pl',
'play-internet.pl',
'centertel.pl',
'satfilm.com.pl',
'odramedia.pl',
'netia.com.pl',
'espol.com.pl',
'netia.com.pl',
'tpnet.pl',
'tpnet.pl',
'tpnet.pl',
'tpnet.pl',
'vectranet.pl',
'vectranet.pl',
'tktelekom.pl',
'tktelekom.pl',
'sitel.net.pl',
'inter-sat.pl',
'mm.pl',
'mm.pl',
'chello.pl',
'chello.pl',
'chello.pl',
'chello.pl',
'chello.pl',
'net-system.pl',
'vectranet.pl',
'krosoft.pl',
'artcom.pl',
'play-internet.pl',
'protonet.pl',
'plus.pl',
'tpnet.pl'
);

my %pcounts;
$pcounts{$_}++ for @cprov;

for my $key ( sort { $pcounts{$b} cmp $pcounts{$a} } keys %pcounts ) {
    print "$key - $pcounts{$key}\n";
}

Upvotes: 0

Views: 58

Answers (1)

ikegami
ikegami

Reputation: 385809

When comparing lexically, 10 comes before 2 just like ba comes before c. You want to compare numerically, so use <=> instead of cmp.

Upvotes: 3

Related Questions