Приглашаем посетить
Кулинария (cook-lib.ru)

Name Sort

#!/usr/local/bin/perl

@names = (' Craig Patchett', 'Walt Disney ', 'Matt Wright', ' Michael W. Smith ', 'George', '  Martha');

@ordered_names = sort by_last_name @names;
foreach $name (@ordered_names) { print "$name\n" }

# Sort subroutine

sub by_last_name {
	
	# Strip trailing and leading spaces
	
	$a =~ s/(^\s+)|(\s+$)//g;
	$b =~ s/(^\s+)|(\s+$)//g;
	
	# Find start of last name (set to end of string if no last name)
	
	$a_last = (rindex($a, " ") + 1) || length($a);
	$b_last = (rindex($b, " ") + 1) || length($b);
	
	# Compare the two (compare full name if last names match)
	
	(substr($a, $a_last) cmp substr($b, $b_last)) || ($a cmp $b);
}