Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Resorting to Sorting

by japhy (Canon)
on Nov 30, 2001 at 23:48 UTC ( #128722=perltutorial: print w/ replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
      @sorted = sort { $a <=> $b } @numbers;  # ascending order
      @sorted = sort { $b <=> $a } @numbers;  # descending order
    
  2. or download this
      @sorted = sort { $a cmp $b } @unsorted;
    
  3. or download this
      @sorted = sort @unsorted;
    
  4. or download this
      @sorted = sort { lc($a) cmp lc($b) } @unsorted;
      # or
      @sorted = sort { uc($a) cmp uc($b) } @unsorted;
    
  5. or download this
      sub age_or_name {
        my ($name_a, $age_a) = split /_/ => $a;
    ...
      @sorted = sort age_or_name @people;
      # @sorted is now
      #   qw( Jon_14 Tim_14 Ray_18 Greg_19 Jeff_19 Joan_20 )
    
  6. or download this
      @sorted = sort { ... } @strings;
    
  7. or download this
      @nodes = (
        { id => 17, size => 300, keys => 2, cmp => 'keys' },
    ...
        { id => 31, size => 2045, keys => 43, cmp => 'keys' },
        { id => 28, size => 6, keys => 0, cmp => 'id' },
      );
    
  8. or download this
      {
        my %cache;  # cache hash is only seen by this function
    ...
      
      @people = qw( Jeff_19 Jon_14 Ray_18 Tim_14 Joan_20 Greg_19 );
      @sorted = sort age_or_name @people;
    
  9. or download this
      {
        my %cache;
    ...
          # compare as needed
        }
      }
    
  10. or download this
      # sorts in-place (meaning @list gets changed)
      # set $unknown to true to indicate variable length
      radix_sort(\@list, $unknown);
    
  11. or download this
      sub radix_sort {
        my ($data, $k) = @_;
    ...
          @$data = map @$_, @buckets;  # expand array refs
        }
      }
    
  12. or download this
      sub radix_sort (\@;$);
      
    ...
      sub radix_sort (\@;$) {
        # ...
      }
    
  13. or download this
      @names  = qw( Jeff Jon Ray Tim Joan Greg );
      @ages   = qw( 19   14  18  14  20   19   );
      @gender = qw( m    m   m   m   f    m    );
    
  14. or download this
      @names  = qw( Jon Tim Ray Greg Jeff Joan );
      @ages   = qw( 14  14  18  19   19   20   );
      @gender = qw( m   m   m   m    m    f    );
    
  15. or download this
      sub age_or_name {
        return (
    ...
          $names[$a] cmp $names[$b]
        )
      }
    
  16. or download this
      @idx = sort age_or_name 0 .. $#ages;
      print "@ages\n";        # 19 14 18 14 20 19
      print "@idx\n";         #  1  3  2  5  0  4
      print "@ages[@idx]\n";  # 14 14 18 19 19 20
    
  17. or download this
      @sorted =
        map { get_original_data($_) }
        sort { ... }
        map { transform_data($_) }
        @original;
    
  18. or download this
      username:password:shell:name:dir
    
  19. or download this
      @sorted =
        map { $_->[0] }
    ...
        }
        map { [ $_, split /:/ ] }
        @entries;
    
  20. or download this
      @transformed = map { [ $_, split /:/ ] } @entries;
    
  21. or download this
      for (@entries) {
        push @transformed, [ $_, split /:/ ];
      }
    
  22. or download this
      @transformed = sort {
        $a->[3] cmp $b->[3]
    ...
        or
        $a->[1] cmp $b->[1]
      } @transformed;
    
  23. or download this
      @sorted = map { $_->[0] } @transformed;
    
  24. or download this
      @sorted =
        map { restore($_) }
        sort
        map { normalize($_) }
        @original;
    
  25. or download this
      my $nulls = 0;
      
    ...
          $nulls = length($1) if length($1) > $nulls;
        }
      }
    
  26. or download this
      $NUL = "\0" x ++$nulls;
    
  27. or download this
      # "\L...\E" is like lc(...)
      @normalized = map { "\L$_\E$NUL$_" } @original;
    
  28. or download this
      @sorted = sort @normalized;
    
  29. or download this
      @sorted = map { (split /$NUL/)[1] } @original;
    
  30. or download this
      # implement our for loop from above
      # as a function
    ...
        sort
        map { "\L$_\E$NUL$_" }
        @original;
    
  31. or download this
      # see Exercise 1 for this function
      $maxlen = maxlen(\@original);
    
  32. or download this
      @sorted =
        map { substr($_, $maxlen) }
        sort
        map { lc($_) . ("\0" x ($maxlen - length)) . $_ }
        @original;
    
  33. or download this
      @sorted =
        {
    ...
        or
        $a->[1] cmp $b->[1]
      }
    
  34. or download this
      #!/usr/bin/perl -w
    
  35. or download this
      package Sorting;
    
  36. or download this
      sub passwd_cmp {
        $a->[3] cmp $b->[3]
    ...
        or
        $a->[1] cmp $b->[1]
      }
    
  37. or download this
      sub case_insensitive_cmp {
        lc($a) cmp lc($b)
      }
    
  38. or download this
      package main;
    
  39. or download this
      @strings = sort Sorting::case_insensitive_cmp
        qw( this Mine yours Those THESE nevER );
    
  40. or download this
      print "<@strings>\n";
    
  41. or download this
      __END__
      <this Mine yours Those THESE nevER>
    
  42. or download this
      #!/usr/bin/perl -w
    
  43. or download this
      package Sorting;
    
  44. or download this
      sub passwd_cmp ($$) {
        local ($a, $b) = @_;
    ...
        or
        $a->[1] cmp $b->[1]
      }
    
  45. or download this
      sub case_insensitive_cmp ($$) {
        local ($a, $b) = @_;
        lc($a) cmp lc($b)
      }
    
  46. or download this
      package main;
    
  47. or download this
      @strings = sort Sorting::case_insensitive_cmp
        qw( this Mine yours Those THESE nevER );
    
  48. or download this
      print "<@strings>\n";
    
  49. or download this
      __END__
      <Mine nevER THESE this Those yours>
    

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://128722]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-12-25 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls