[tpm] OID sorting

Viktor Pavlenko vvp at cogeco.ca
Sat May 2 20:17:12 PDT 2009


>>>>> "FH" == Fulko Hew <fulko.hew at gmail.com> writes:

    >> my @list = (
    >> "1.3",
    >> "1.2.3",
    >> "1.2.3.1",
    >> "1.2.4.1",
    >> "1.2.30",
    >> "1",
    >> "1.2",
    >> );
    >> # The expected sort order is the same as the order above
    >> 

    FH> The expected order for the above set is:

    FH> 1
    FH> 1.2
    FH> 1.2.3
    FH> 1.2.3.1
    FH> 1.2.4.1
    FH> 1.2.30
    FH> 1.3

Following the KISS directive:

----------------------------------------------------------------->8
#!/usr/bin/perl -wl

use strict;
$, = "\n";

my @list = (
    "1.3",
    "1.2.3",
    "1.2.3.1",
    "1.2.4.1",
    "1.2.30",
    "1",
    "1.2",
);

sub srt
{
    my ($a1, $a2) = map { [ split /\./ ] } @_;
    for (my $i = 0; $i <= $#$a1; ++$i) {
        return 1 unless defined $a2->[$i];
        return $a1->[$i] <=> $a2->[$i] unless $a1->[$i] == $a2->[$i];
    }
    return 0;
}

print sort { srt($a, $b) } @list, "\n";
----------------------------------------------------------------->8

-- 
Viktor



More information about the toronto-pm mailing list