? apachesolr_date_sort_options.patch
Index: apachesolr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v
retrieving revision 1.1.2.12.2.155.2.117
diff -u -p -r1.1.2.12.2.155.2.117 apachesolr.module
--- apachesolr.module	5 Jan 2011 19:56:05 -0000	1.1.2.12.2.155.2.117
+++ apachesolr.module	27 Jan 2011 18:59:13 -0000
@@ -1171,6 +1171,7 @@ function apachesolr_facet_block($respons
  */
 function apachesolr_date_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $facet_callback = FALSE) {
   $items = array();
+  $items_addtl = array();
 
   $new_query = clone $query;
   foreach (array_reverse($new_query->get_filters($facet_field)) as $filter) {
@@ -1184,7 +1185,7 @@ function apachesolr_date_facet_block($re
       $facet_text = apachesolr_date_format_iso_by_gap(apachesolr_date_find_query_gap($filter['#start'], $filter['#end']), $filter['#start']);
     }
     $options['query'] = $new_query->get_url_queryvalues();
-    array_unshift($items, theme('apachesolr_unclick_link', $facet_text, $new_query->get_path(), $options));
+    $items[strtotime($filter['#start'])] = theme('apachesolr_unclick_link', $facet_text, $new_query->get_path(), $options);  
   }
   // Add links for additional date filters.
   if (!empty($response->facet_counts->facet_dates->$facet_field)) {
@@ -1222,13 +1223,46 @@ function apachesolr_date_facet_block($re
       $new_query = clone $query;
       $new_query->add_filter($facet_field, '['. $facet .' TO '. $range_end[$facet] .']');
       $options['query'] = $new_query->get_url_queryvalues();
-      $items[] = theme('apachesolr_facet_link', $facet_text, $new_query->get_path(), $options, $count, FALSE, $response->response->numFound);
+      $items_addtl[strtotime($facet)] = theme('apachesolr_facet_link', $facet_text, $new_query->get_path(), $options, $count, FALSE, $response->response->numFound);      
     }
   }
-  if (count($items) > 0) {
+  if (count($items) > 0 || count($items_addtl) > 0) {
     // 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);
+
+    /**
+     * SORT DATE FACETS IN SPECIFIED ORDER. String sorting doesn't really make sense, here, unless someone wants
+     * to sort by date part names (e.g., January, February, Monday, Tuesday, etc.). We've left the switches on those
+     * sort options intact in case we want to add this in. But, for now, we've changed the sort flags to treat all 
+     * sorts numerically. Also note that we're sorting unclick and click links independently of one another.
+     */    
+    $facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array());
+    switch ($facet_query_sorts[$module][$delta]) {
+      case 'index numeric asc':
+        ksort($items, SORT_NUMERIC);
+        ksort($items_addtl, SORT_NUMERIC);
+        break;
+      case 'index numeric desc':
+        krsort($items, SORT_NUMERIC);
+        krsort($items_addtl, SORT_NUMERIC);        
+        break;
+      case 'index desc':
+      case 'index key desc':
+        krsort($items, SORT_NUMERIC);
+        krsort($items_addtl, SORT_NUMERIC);
+        break;
+      case 'index asc':
+      case 'index key asc':
+      default:
+        ksort($items, SORT_NUMERIC);
+        ksort($items_addtl, SORT_NUMERIC);
+        break;
+    }
+    
+    $items += $items_addtl;
+    array_keys($items);    
+    
     $output = theme('apachesolr_facet_list', $items, $limit, $delta);
     return array('subject' => $filter_by, 'content' => $output);
   }
@@ -1415,27 +1449,26 @@ function apachesolr_facetcount_form($mod
 
   // 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('Alphabetical, ascending'),
-        'index desc' => t('Alphabetical, descending'),
-        'index numeric asc' => t('Numeric, ascending'),
-        'index numeric desc' => t('Numeric, descending'),
-        'index key asc' => t('Key sort, ascending'),
-        'index key desc' => t('Key sort, 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. %Alphabetical will sort alphabetically, and %Numeric numerically, either ascending or descending.', array(
-        '%Count' => t('Count'),
-        '%Alphabetical' => t('Alphanumeric'),
-        '%Numeric' => t('Numeric'),
-      )),
-      '#default_value' => isset($sorts[$module][$delta]) ? $sorts[$module][$delta] : 'count',
-    );
-  }
+  $form['apachesolr_facet_query_sort'] = array(
+    '#type' => 'radios',
+    '#title' => t('Sort order of facet links'),
+    '#options' => array(
+      'count' => t('Count'),
+      'index asc' => t('Alphabetical, ascending'),
+      'index desc' => t('Alphabetical, descending'),
+      'index numeric asc' => t('Numeric, ascending'),
+      'index numeric desc' => t('Numeric, descending'),
+      'index key asc' => t('Key sort, ascending'),
+      'index key desc' => t('Key sort, 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. %Alphabetical will sort alphabetically, and %Numeric numerically, either ascending or descending.', array(
+      '%Count' => t('Count'),
+      '%Alphabetical' => t('Alphanumeric'),
+      '%Numeric' => t('Numeric'),
+    )),
+    '#default_value' => isset($sorts[$module][$delta]) ? $sorts[$module][$delta] : 'count',
+  );
+  
   $form['apachesolr_facet_missing'] = array(
     '#type' => 'radios',
     '#title' => t('Include a facet for missing'),
