Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: a sort question

by varian (Chaplain)
on Jun 07, 2007 at 09:20 UTC ( [id://619772]=note: print w/replies, xml ) Need Help??


in reply to a sort question

Just enhance the sort to work on arrays, something like this would do it:

#!/usr/bin/perl use strict;use warnings; sub versionsort { my ($a,$b) = @_; my $diff; foreach (0..$#$a) { return 1 if !defined $b->[$_]; $diff = $a->[$_] <=> $b->[$_]; return $diff if $diff; } return @$a <=> @$b; } my @data = qw(1.1 11.2.3 2.3 11.2.1 11.2 11.2.1.1); print "before: ".join(', ',@data)."\n"; my @versions = map { [split '\.',$_] } @data; my @sorted = map { join '.',@$_} sort {versionsort($a,$b)} @versions; print "sorted: ".join(', ',@sorted)."\n";
Outputs:
before: 1.1, 11.2.3, 2.3, 11.2.1, 11.2, 11.2.1.1 sorted: 1.1, 2.3, 11.2, 11.2.1, 11.2.1.1, 11.2.3

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://619772]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-25 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found