Chinaunix首页 | 论坛 | 博客
  • 博客访问: 838236
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-17 09:18:04

  1. my %score = ("barney" => 195, "fred" => 205, "dino" => 30);
  2. @new_values = sort {$score{$b} <=>$score{ $a}} keys %score;
  3. print @new_values;
fredbarneydino

if there are two equal values in the hash my %score = ("barney" => 195, "fred" => 205,
  "dino" => 30, "bamm-bamm" => 195,); and we want them be in ASCIIbetical order. the sort function should be
  1. {$score{$b}<=>$score{$a} or $a cmp $b}
if the spaceship sees two different scores, that’s the com-parison we want to use. It returns −1 or 1, a true value, so the low-precedence short-circuit or will mean that the rest of the expression will be skipped, and the comparison we want is returned.
But if the spaceship sees two identical scores, it returns 0, a false value, and thus
the cmp operator gets its turn at bat, returning an appropriate ordering value considering
the keys as strings.

There’s no reason that your sort subroutine has to be limited to two levels of sorting,
of course. Here the Bedrock library program puts a list of patron ID numbers in order
according to a five-level sort.† This example sorts according to the amount of each
patron’s outstanding fines (as calculated by a subroutine &fines, not shown here), the
number of items they currently have checked out (from %items), their name (in order
by family names, then by personal name, both from hashes), and finally by the patron’s
ID number, in case everything else is the same:
@patron_IDs = sort {
  &fines($b) <=> &fines($a) or
  $items{$b} <=> $items{$a} or
  $family_name{$a} cmp $family_name{$a} or
  $personal_name{$a} cmp $family_name{$b} or
  $a <=> $b
} @patron_IDs;





阅读(367) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~