+sub get_sort_function {
+ my $method = $_[0];
+
+ if ($method =~ m/\s/) {
+ my @methods = map { get_sort_function($_) } split(' ', $method);
+
+ return sub {
+ foreach my $method (@methods) {
+ my $answer = $method->($_[0], $_[1]);
+ return $answer if $answer;
+ }
+
+ return 0;
+ };
+ }
+
+ my $sense = 1;
+
+ if ($method =~ s/^-//) {
+ $sense = -1;
+ }
+
+ my $token = $method;
+ my $parameter = undef;
+
+ if ($method =~ m/^(\w+)\((.*)\)$/) {
+ $token = $1;
+ $parameter = $2;
+ }
+
+ if (exists $hooks{sort}{$token}{call}) {
+ my $callback = $hooks{sort}{$token}{call};
+ return sub { $sense * $callback->($_[0], $_[1], $parameter) };
+ }
+
+ if ($method eq 'title') {
+ return sub { $sense * (pagetitle(basename($_[0])) cmp pagetitle(basename($_[1]))) };
+ }
+
+ if ($method eq 'title_natural') {
+ eval q{use Sort::Naturally};
+ if ($@) {
+ error(gettext("Sort::Naturally needed for title_natural sort"));
+ }
+ return sub { $sense * Sort::Naturally::ncmp(pagetitle(basename($_[0])), pagetitle(basename($_[1]))) };
+ }
+
+ if ($method eq 'mtime') {
+ return sub { $sense * ($pagemtime{$_[1]} <=> $pagemtime{$_[0]}) };
+ }
+
+ if ($method eq 'age') {
+ return sub { $sense * ($pagectime{$_[1]} <=> $pagectime{$_[0]}) };
+ }
+
+ error sprintf(gettext("unknown sort type %s"), $method);
+}
+