numeric facets with invalid data sort fix
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 26 May 2010 13:00:32 +0000 (15:00 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 26 May 2010 13:00:32 +0000 (15:00 +0200)
This doesn't actually produce correct sort order, but we work on
GIGO principle: garbage in - garbage out

If you filter just valid numeric values sorting will work, and corrupted
sort order as it is now is clear indication that your data isn't clean

lib/MojoFacets/Data.pm

index 8587d9f..65aef48 100644 (file)
@@ -606,16 +606,19 @@ sub facet {
        $sort ||= $numeric ? 'a' : 'c';
 
        @facet_names = sort {
-               if ( $sort =~ m/a/i ) {
-                       $numeric ? $a <=> $b : lc $a cmp lc $b;
-               } elsif ( $sort =~ m/d/i ) {
-                       $numeric ? $b <=> $a : lc $b cmp lc $a;
-               } elsif ( $sort =~ m/c/i ) {
-                       ( $facet->{$b} || -1 ) <=> ( $facet->{$a} || -1 )
+               my $result;
+               if ( $sort eq 'a' ) {
+                       $result = $numeric ? $a <=> $b : lc $a cmp lc $b;
+               } elsif ( $sort eq 'd' ) {
+                       $result = $numeric ? $b <=> $a : lc $b cmp lc $a;
+               } elsif ( $sort eq 'c' ) {
+                       $result = ( $facet->{$b} || -1 ) <=> ( $facet->{$a} || -1 )
                } else {
                        warn "unknown sort: $sort";
-                       $a cmp $b;
+                       $result = $a cmp $b;
                }
+               $result = $a cmp $b unless defined $result; # FIXME cludge for numeric facets with invalid data
+               $result;
        } @facet_names;
 
        $self->render( name => $name, facet => $facet, checked => $checked,