home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / perl / 7599 < prev    next >
Encoding:
Text File  |  1992-12-23  |  1.3 KB  |  43 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!noc.near.net!meiko.com!mike
  3. From: mike@meiko.com (Mike Stok)
  4. Subject: Re: Looking for a "numerical/alpha" sort routine.
  5. Message-ID: <1992Dec23.213758.11860@meiko.com>
  6. Sender: news@meiko.com
  7. Organization: Meiko Scientific Corp.
  8. References: <1992Dec23.193120.25893@pool.info.sunyit.edu>
  9. Date: Wed, 23 Dec 1992 21:37:58 GMT
  10. Lines: 31
  11.  
  12. In article <1992Dec23.193120.25893@pool.info.sunyit.edu> buck@pool.info.sunyit.edu (Jesse Buckley) writes:
  13. >Does anyone have a sort/compare routine that will work for numerical
  14. >alpha data?  For example, if I have 1, 2, 3a, 3b, 3c, I want them sorted
  15. >that way.  Right now the <=> compare ignored the letters after the
  16. >numbers.  Thanks.
  17.  
  18. You could try something like this, though pattern matching each time
  19. you do the comparison may be heavy on cpu...
  20.  
  21. #!/usr/bin/perl
  22.  
  23. foreach $element (sort mySort ('1', '2', '3', '2', '1a', '2b',
  24.                    '5c', '1d', '1b', '1e', '2a', '7'))
  25. {
  26.   print "$element\n";
  27. }
  28.  
  29. sub mySort
  30. {
  31.   local ($aNum, $aLet) = ($a =~ /(\d+)([a-z]*)/);
  32.   local ($bNum, $bLet) = ($b =~ /(\d+)([a-z]*)/);
  33.  
  34.   $aLet cmp $bLet unless $aNum <=> $bNum;
  35. }
  36.  
  37. Mike
  38. --
  39. The "usual disclaimers" apply.    |
  40. Mike Stok                         |
  41. mike@meiko.com                    |
  42. Meiko tel: (617) 890 7676         |
  43.