Please send questions to st10@humboldt.edu .
#!/usr/bin/perl -w

#######################################################
# lect08_sort1
#
# advanced sorting, take 1!
#    sorting via sort-subroutine;
#
# modified by Sharon Tuttle from "Learning Perl",
#    by Schartz and Phoenix
#
# last modified: 10-11-04
#######################################################

#----------------------------------------------------
# print a list of 3-character values right-justified,
#    one value per line
#----------------------------------------------------

sub print3_and_pause
{
    my @list = @_;
    my $item;

    foreach (@list)
    {
        $item = $_;
        printf "%3s\n", $item;
    }
    print "\n";

    print "type anything and enter to continue: ";
    my $dummy = <STDIN>;
    return;
}

#-----
# action starts here...
#---

my @jumbled_nums = (1, 8, 2, 200, 10, 9, 6);

print "\n";
print "ASCIIbetical default that plain 'ol sort gives you:\n";
print "----------------------------------------------------\n";

&print3_and_pause( sort @jumbled_nums );

#------------------------------------------------
# "long" form --- numeric sort subroutine
#------------------------------------------------

sub by_number1
{
    if ($a < $b)
    {
        return -1;
    }
    elsif ($a > $b)
    {
 	return 1;
    }
    else
    {
        return 0;
    }
}

my @result = sort by_number1 @jumbled_nums;

print "\n";
print "results from sort using by_number1:\n";
print "----------------------------------------------------\n";

&print3_and_pause( @result );

#------------------------------------------------
# "short" form --- numeric sort subroutine using
#    spaceship perator
#------------------------------------------------

sub by_number2
{
    $a <=> $b   # really? no ; ?????
}

@result = sort by_number2 @jumbled_nums;

print "\n";
print "results from sort using by_number2:\n";
print "----------------------------------------------------\n";

&print3_and_pause( @result );

#-----
# and, using the spaceship operator directly in sort call:
#-----

# STYLE NOTE!!!
#    we'll say that, as long as it fits EASILY, 
#    the below is acceptable indentation.
# (if it is longer? I expect the usual { } placement
#    discussed in the course style standards!!)

@result = sort {$a <=> $b} @jumbled_nums;

print "\n";
print "results from sort using in-line <=>:\n";
print "----------------------------------------------------\n";

&print3_and_pause( @result );

#-----
# and, a reverse-numeric-ordering example...
#-----

@result = sort {$b <=> $a} @jumbled_nums;

print "\n";
print "results from sort using in-line \$b <=> \$a:\n";
print "----------------------------------------------------\n";

&print3_and_pause( @result );

#-----
# and, a reverse-ASCIIbetical-ordering example...
#-----

@result = sort {$b cmp $a} @jumbled_nums;

print "\n";
print "results from sort using in-line \$b cmp \$a:\n";
print "----------------------------------------------------\n";

&print3_and_pause( @result );


# end of lect08_sort1