in reply to Re^3: Custom sort with string of numbers
in thread Custom sort with string of numbers

I figure that the basic sort algorithm just got a lot smarter and reduced the number of comparisons.
AFAIK, the latest significant change in sorting happened in 5.6.0, when Perl changed to using mergesort by default, instead of quicksort (and introduced a pragma to switch). Also, when quicksort is being used, and the array is larger than a certain treshold, the array is shuffled first. Before, it was rather easy to generate an input that took quadratic time to sort (for instance, a list of the form 1 .. $N, reverse(1 .. $N) took quadratic time).

The mergesort that has been implemented is pretty nifty, in the sense that it takes advantage of (reverse) sorted sublists. This makes it fast on almost sorted lists (the Achilles heel of traditional quicksort). It's also stable.

However, all this happened a decade ago.

These various techniques like "Schwartzian Transform" are still applicable and they still improve performance when doing very large sorts.
Yes, and in almost all cases, a GRT is faster than an ST. However, considering the number of chapters/subchapters/subsubchapters in a book, I doubt the difference is really going to matter.

Since the sort is stable, there's an alternative way of sorting the data:

@data = map {join "-", @$_} sort {$a->[0] <=> $b->[0]} sort {$a->[1] <=> $b->[1]} sort {$a->[2] <=> $b->[2]} map {[split /-/]} @data;
You won't be able to do that with Perl's quicksort implementation.

Here's another way to sort the given data. It uses neither mergesort, nor quicksort:

my @d = map {[split /-/]} @data; foreach my $i (reverse (0 .. 2)) { my @t; push @{$t[$$_[$i]]}, $_ for @d; @d = map {@{$_ || []}} @t; } @data = map {join "-", @$_} @d;