#!/usr/bin/perl 
use warnings;
use strict;

# playing with sorting

my @data = ( [10,"jim"],
	     [20,"fred"],
	     [5,"sam"],
	   );
# code block should return a comparison between two elements $a and $b from list.
# Here I'm doing a numeric sort on the first piece of each thing in @data.
my @number_sorted = sort { $a->[0] <=> $b->[0]  }  @data;
#
#
my @letter_sorted = sort { $a->[1] cmp $b->[1]  }  @data;

# --------------------

my @names = ( "Jim Mahoney",
	      "Al Einstein",
	      "Wiley Coyote",
	    );
print " names = (",join(", ",@names),")\n";

# my @temp = map { [ (split(/ /,$_))[1], $_ ] }   @names;
# use Data::Dumper;
# print Dumper([@temp]);
# my @temp_sorted = sort { $a->[0] cmp $b->[0] } @temp;
# my @temp2 = map { $_->[1] } @temp_sorted;
# print join(',',@temp2), "\n";

# --------------------------

my @sorted = map { $_->[1] } 
             sort { $a->[0] cmp $b->[0] } 
             map { [ (split(/ /,$_))[1], $_ ] }
             @names;
print " sorted by last name = (",join(", ",@sorted),")\n";

# OR this, which is slower because the extraction is done multiple times.
@sorted = sort { (split(/ /,$a))[1] cmp (split(/ /,$b))[1] } @names



