home *** CD-ROM | disk | FTP | other *** search
/ ftp.cse.unsw.edu.au / 2014.06.ftp.cse.unsw.edu.au.tar / ftp.cse.unsw.edu.au / pub / doc / languages / perl / nutshell / ch4 / sort < prev    next >
Encoding:
Text File  |  1992-10-18  |  552 b   |  20 lines

  1. sub numerically { $a <=> $b; }
  2. @sortedbynumber = sort numerically 53,29,11,32,7;
  3.  
  4. sub byage {
  5.     $age{$a} <=> $age{$b};
  6. }
  7. @sortedclass = sort byage @class;
  8.  
  9. sub reverse { $b cmp $a; }
  10. @harry = ('dog', 'cat', 'x', 'Cain', 'Abel');
  11. @george = ('gone', 'chased', 'yz', 'Punished', 'Axed');
  12. print sort @harry;
  13.     # prints AbelCaincatdogx
  14. print sort reverse @harry;
  15.     # prints xdogcatCainAbel
  16. print reverse sort @harry;
  17.     # prints xdogcatCainAbel
  18. print sort @george, 'to', @harry;    # Remember, it's a LIST.
  19.     # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
  20.