Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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