SPUG: sorting hierarchical section numbers

John Subaykan john.spug at subaykan.com
Thu Aug 10 23:16:49 PDT 2006


Can't you just do something like this? 

my @array2 = sort { outlinish() } @array1;

sub outlinish {

    my @one = split /\./, $a;
    my @two = split /\./, $b;

    my $max = @one > @two ? @one : @two;

    for my $i (0 .. $max-1) {
        if (my $res = ($one[$i] || 0) <=> ($two[$i] || 0)) {
            return $res;
        }
    }
}

# and maybe add some schmantzy memoization technique for efficiency

- John (waiting for Yitzchak or Colin to tell my why this is bad!)


Michael R. Wolf wrote:
> I want to sort strings that represent outline numbers.  
>
> 1
> 1.1
> 1.2
> 1.3
> 1.3.1
> 1.3.2
> 1.3.2.1
> 1.2



More information about the spug-list mailing list