Results 1 to 3 of 3
  1. #1
    Join Date
    May 2004
    Posts
    5

    Unanswered: Sort Alpha Array

    Hi!!
    I have a little problem
    I want to sort an alphanumerical array:
    For example:
    ("A10","A1","A20","A2","A11","AB1","A19","A32","A3 ","B5","B10","AF5")
    sorted array will be:
    ("A1","A2","A3","A10","A11","A19","A20","A32","AB1 ","AF5","B5","B10")
    How can i do that???
    Thanks a lot

  2. #2
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    The way I read it, you want to "break" the "value" into two components - an alpha component and a numeric component. The values are to be ordered alphabetically with respect to the alpha components and numerically with respect to the numeric components. Is this correct?

  3. #3
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    This what you want?

    Code:
    my(@out)=sort {
      my($aA,$aN,$bA,$bN);
      if ($a =~ /([a-zA-Z]+)(\d+)/) { $aA=$1;$aN=$2;};
      if ($b =~ /([a-zA-Z]+)(\d+)/) { $bA=$1;$bN=$2;};
      if ($aA lt $bA) { return -1;}
      elsif ($aA gt $bA) { return 1;}
      else { return $aN <=> $bN};
       } ("A10","A1","A20","A2","A11","AB1","A19","A32","A3","B5","B10","AF5");
    Last edited by senza_nome; 06-21-04 at 16:32.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •