Link to home
Start Free TrialLog in
Avatar of uberdruck
uberdruck

asked on

Perl Sort on associative arrays

Hello,

I want a sort on keys of a associative array. the keys are like 5, 10, 20, 30

I want to show them and the value associated to these keys, but in the ordinal order.
but my sort show: 10 20 30 5

because 1 2 3 5, it sorts this like string, but i want it sorted numericaly!

look at my code:

--------------------------------------------
      my      %liste_sequences = ();
    while( $db->FetchRow() ) {
             %hash = $db->DataHash;
               $liste_sequences{$hash{ordre_affichage}} = $hash{sequence};
      }

      $compteur = 10;
      foreach $ordre_aff (sort(keys(%liste_sequences))) {
                  print "--> $ordre_aff <br>";
        }
---------------------------------------------

Thanx a lot
Avatar of jhurst
jhurst

You will need to create  your own sort function or modify your keys.  Personaly I would do the former.  I would make the keys space filled numbers.
ASKER CERTIFIED SOLUTION
Avatar of bcladd
bcladd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is exactly the type of 'your own' sort function that I had in mind, except that you will need to ensure that the sort is a numeric sort and not a text/ascii character sort