home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!noc.near.net!meiko.com!mike
- From: mike@meiko.com (Mike Stok)
- Subject: Re: Looking for a "numerical/alpha" sort routine.
- Message-ID: <1992Dec23.213758.11860@meiko.com>
- Sender: news@meiko.com
- Organization: Meiko Scientific Corp.
- References: <1992Dec23.193120.25893@pool.info.sunyit.edu>
- Date: Wed, 23 Dec 1992 21:37:58 GMT
- Lines: 31
-
- In article <1992Dec23.193120.25893@pool.info.sunyit.edu> buck@pool.info.sunyit.edu (Jesse Buckley) writes:
- >Does anyone have a sort/compare routine that will work for numerical
- >alpha data? For example, if I have 1, 2, 3a, 3b, 3c, I want them sorted
- >that way. Right now the <=> compare ignored the letters after the
- >numbers. Thanks.
-
- You could try something like this, though pattern matching each time
- you do the comparison may be heavy on cpu...
-
- #!/usr/bin/perl
-
- foreach $element (sort mySort ('1', '2', '3', '2', '1a', '2b',
- '5c', '1d', '1b', '1e', '2a', '7'))
- {
- print "$element\n";
- }
-
- sub mySort
- {
- local ($aNum, $aLet) = ($a =~ /(\d+)([a-z]*)/);
- local ($bNum, $bLet) = ($b =~ /(\d+)([a-z]*)/);
-
- $aLet cmp $bLet unless $aNum <=> $bNum;
- }
-
- Mike
- --
- The "usual disclaimers" apply. |
- Mike Stok |
- mike@meiko.com |
- Meiko tel: (617) 890 7676 |
-