--- apachesolr/apachesolr.module	2009-11-20 05:37:36.000000000 -0600
+++ apachesolr_patched/apachesolr.module	2009-11-20 13:21:43.000000000 -0600
@@ -811,6 +811,7 @@ function apachesolr_block_admin_configur
  */
 function apachesolr_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {
   if (!empty($response->facet_counts->facet_fields->$facet_field)) {
+    $facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array());
     $contains_active = FALSE;
     $items = array();
     foreach ($response->facet_counts->facet_fields->$facet_field as $facet => $count) {
@@ -834,6 +835,10 @@ function apachesolr_facet_block($respons
       if ($facet_callback && function_exists($facet_callback)) {
         $facet_text = $facet_callback($facet, $options);
       }
+      // If this block is to be alphabetically sorted, change $sortpre.
+      if (isset($facet_query_sorts[$module][$delta]) && ($facet_query_sorts[$module][$delta] != 'count')) {
+        $sortpre = $facet_text;
+      }
       $unclick_link = '';
       $active = FALSE;
       $new_query = clone $query;
@@ -856,7 +861,12 @@ function apachesolr_facet_block($respons
     }
     // Unless a facet is active only display 2 or more.
     if ($items && ($response->response->numFound > 1 || $contains_active)) {
-      ksort($items, SORT_STRING);
+      if (!isset($facet_query_sorts[$module][$delta]) || ($facet_query_sorts[$module][$delta] == 'index asc')) {
+        ksort($items, SORT_STRING);
+      }
+      else if ($facet_query_sorts[$module][$delta] == 'index desc') {
+        krsort($items, SORT_STRING);
+      }
       // Get information needed by the rest of the blocks about limits.
       $initial_limits = variable_get('apachesolr_facet_query_initial_limits', array());
       $limit = isset($initial_limits[$module][$delta]) ? $initial_limits[$module][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10);
@@ -1094,6 +1104,7 @@ function apachesolr_date_gap_drilldown($
 function apachesolr_facetcount_form($module, $delta) {
   $initial = variable_get('apachesolr_facet_query_initial_limits', array());
   $limits = variable_get('apachesolr_facet_query_limits', array());
+  $sorts = variable_get('apachesolr_facet_query_sorts', array());
   $facet_missing = variable_get('apachesolr_facet_missing', array());
 
   $limit = drupal_map_assoc(array(50, 40, 30, 20, 15, 10, 5, 3));
@@ -1113,6 +1124,18 @@ function apachesolr_facetcount_form($mod
     '#description' => t('The maximum number of filter links to show in this block.'),
     '#default_value' => isset($limits[$module][$delta]) ? $limits[$module][$delta] : variable_get('apachesolr_facet_query_limit_default', 20),
   );
+  
+  // TODO: Generalize how we know what type a facet block is by putting field
+  // type into the facet definition. 'created' and 'changed' are date blocks.
+  if ($delta != 'created' && $delta != 'changed') {
+    $form['apachesolr_facet_query_sort'] = array(
+      '#type' => 'radios',
+      '#title' => t('Sort order of facet links'),
+      '#options' => array('count' => t('Count'), 'index asc' => t('Alphanumeric, ascending'), 'index desc' => t('Alphanumeric, descending')),
+      '#description' => t('The sort order of facet links in this block. %Count, which is the default, will show facets with the most results first. %Alphanumeric will sort alphabetically, either ascending or descending.', array('%Count' => t('Count'), '%Alphanumeric' => t('Alphanumeric'))),
+      '#default_value' => isset($sorts[$module][$delta]) ? $sorts[$module][$delta] : 'count',
+    );
+  }
   $form['apachesolr_facet_missing'] = array(
     '#type' => 'radios',
     '#title' => t('Include a facet for missing'),
@@ -1134,6 +1157,9 @@ function apachesolr_facetcount_save($edi
   $limits = variable_get('apachesolr_facet_query_limits', array());
   $limits[$module][$delta] = (int)$edit['apachesolr_facet_query_limit'];
   variable_set('apachesolr_facet_query_limits', $limits);
+  $sorts = variable_get('apachesolr_facet_query_sorts', array());
+  $sorts[$module][$delta] = $edit['apachesolr_facet_query_sort'];
+  variable_set('apachesolr_facet_query_sorts', $sorts);
   $initial = variable_get('apachesolr_facet_query_initial_limits', array());
   $initial[$module][$delta] = (int)$edit['apachesolr_facet_query_initial_limit'];
   variable_set('apachesolr_facet_query_initial_limits', $initial);
Only in apachesolr_patched: apachesolr.module.orig
diff -urp apachesolr/apachesolr_search.module apachesolr_patched/apachesolr_search.module
--- apachesolr/apachesolr_search.module	2009-11-20 04:24:30.000000000 -0600
+++ apachesolr_patched/apachesolr_search.module	2009-11-20 13:21:43.000000000 -0600
@@ -422,6 +422,7 @@ function apachesolr_search_add_facet_par
       // which won't be used.
       if (apachesolr_block_visibility($query, $module, $delta)) {
         // TODO: generalize handling of date and range facets.
+        // TODO: put field type in the facet definitions.
         if ($module == 'apachesolr_search' && ($facet_field == 'created' || $facet_field == 'changed')) {
           list($start, $end, $gap) = apachesolr_search_date_range($query, $facet_field);
           if ($gap) {
@@ -738,11 +739,11 @@ function apachesolr_search_block($op = '
 
         // Handle taxonomy vocabulary facets
         if ((strpos($delta, 'im_vid_') === 0) && module_exists('taxonomy')) {
+          $facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array());
 
           if (is_object($response->facet_counts->facet_fields->$delta)) {
             $contains_active = FALSE;
             $terms = array();
-
             foreach ($response->facet_counts->facet_fields->$delta as $tid => $count) {
               $options = array('delta' => $delta);
               if ($tid == '_empty_') {
@@ -752,6 +753,10 @@ function apachesolr_search_block($op = '
               $unclick_link = '';
               unset($active);
               $term = taxonomy_get_term($tid);
+              $sortpre = '';
+              if (isset($facet_query_sorts['apachesolr_search'][$delta]) && ($facet_query_sorts['apachesolr_search'][$delta] != 'count')) {
+                $sortpre = $term->name;
+              }
               $new_query = clone $query;
               if ($active = $query->has_filter('tid', $tid)) {
                 $contains_active = TRUE;
@@ -765,16 +770,24 @@ function apachesolr_search_block($op = '
                 $link = theme('apachesolr_facet_link', $term->name, $new_query->get_path(), $options, $count, $active, $response->response->numFound);
               }
               $countsort = $count == 0 ? '' : 1 / $count;
+              $countsort += .000001;
+              $countsort = number_format($countsort, 6);
               // if numdocs == 1 and !active, don't add.
               if ($response->response->numFound > 1 || $active) {
-                $terms[$term->vid][$active ? $countsort . $term->name : 1 + $countsort . $term->name] = $link;
+                $terms[$term->vid][$active ? $sortpre . $countsort . $term->name : $sortpre . (1 + $countsort) . $term->name] = $link;
               }
             }
           }
           $vid = substr($delta, 7);
           $vocab = taxonomy_vocabulary_load($vid);
           if (is_numeric($vid) && is_array($terms) && isset($terms[$vid]) && is_array($terms[$vid])) {
-            ksort($terms[$vid]);
+            if (!isset($facet_query_sorts['apachesolr_search'][$delta]) || ($facet_query_sorts['apachesolr_search'][$delta] == 'index asc')) {
+              ksort($terms[$vid]);
+            }
+            else if ($facet_query_sorts['apachesolr_search'][$delta] == 'index desc') {
+              krsort($terms[$vid]);
+            }
+            
             $limit = isset($initial_limits['apachesolr_search'][$delta]) ? $initial_limits['apachesolr_search'][$delta] : $limit_default;
             return array(
               'subject' => t('Filter by @name', array('@name' => $vocab->name)),
Only in apachesolr_patched: facet_block_sorting.patch
Only in apachesolr_patched: name_sort.patch
