[Pdx-pm] sort issues.

chromatic chromatic at wgz.org
Fri Aug 30 12:51:09 CDT 2002


On Friday 30 August 2002 10:36, Roth, Gabrielle wrote:

> 1.  Obviously, the order isn't right.  I thought the <=> operator would do
> a numerical sort, but apparently that's just for integers?  I'm not any
> better off than if I just did a regular sort.

1.10 is numerically equivalent to 1.1, which is less than 1.2.  This *is* a 
standard numerical sort.  You will need to create your own sorting function 
that respects the semantics you want.  Off the top of my head, it might be:

sub sort_version
{
	return 0 if $a eq $b;
	my $first = [ split/\./, $a ];
	my $second = [ split /\./, $b ];

	return -1 if $first->[0] < $second->[0];
	return 1 if $first->[0] > $second->[0];
	return -1 if $first->[1] < $second->[1];
	return 1;
}

That's completely untested, and I usually have to look in perldoc -f sort to 
get these right.

Maybe Tom Phoenix will be along shortly with a much better example and 
explanation.  :)

> 2.  One of (1.1|1.10) goes missing;  I thought maybe because the <=>
> evaluated them as equivalent, but it does this with the "cmp" operator as
> well.  I tried quoting the keys, but that didn't help either.

They're numerically equivalent, so they both evaluate to 1.1.  I would be 
highly surprised if you had both in your hash.

I find it very surprising that quoting them didn't make a difference, and 
would like to see an example of this.

-- c



More information about the Pdx-pm-list mailing list