Sorting and undefined values

Earle Martin perl at
Tue Dec 12 17:00:02 GMT 2006


I have this code:

my @sorted = sort {
  $foo->{$a}->{'x'} cmp $foo->{$b}->{'x'} ||
  $foo->{$b}->{'y'} cmp $foo->{$b}->{'y'}
} keys %$foo;

This only produces the keys of items where 'x' is defined. What do I
need to do to sort first on 'x' if it's defined, and then on 'y', or
just 'y' if 'x' is undefined?

Thanks in advance,


Earle Martin

More information about the mailing list