Skip to content

Instantly share code, notes, and snippets.

@benevolent0505
Created August 16, 2016 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benevolent0505/0d1465f8308dd4530aee3253ba251156 to your computer and use it in GitHub Desktop.
Save benevolent0505/0d1465f8308dd4530aee3253ba251156 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
my $people = [
{ first_name => 'Martry', last_name => 'McFly' },
{ first_name => 'Emmett', last_name => 'Brown' },
{ first_name => 'Biff', last_name => 'Tannen'},
];
print "sort by first name\n";
foreach my $human (sort { $a->{first_name} cmp $b->{first_name} } @$people) {
print "$human->{first_name} $human->{last_name}\n";
}
print "\n";
print "sort by last name\n";
foreach my $human (sort { $a->{last_name} cmp $b->{last_name} } @$people) {
print "$human->{first_name} $human->{last_name}\n";
}
# sort by first name
# Biff Tannen
# Emmett Brown
# Martry McFly
#
# sort by last name
# Emmett Brown
# Martry McFly
# Biff Tannen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment