Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.775
diff -u -p -r1.775 node.module
--- modules/node/node.module	13 Jan 2007 10:21:31 -0000	1.775
+++ modules/node/node.module	13 Jan 2007 15:05:09 -0000
@@ -859,6 +859,23 @@ function node_search($op = 'search', $ke
         $keys = search_query_insert($keys, 'category');
       }
 
+      $orderby = 'ORDER BY score DESC';
+      if ($sort = search_query_extract($keys, 'sort')) {
+	/* do NOT trust that the submitted sort order is safe */
+	switch ($sort) {
+	case 'score':
+	  $orderby = 'ORDER BY score DESC';
+	  break;
+	case 'oldest':
+	  $orderby = "ORDER BY n.created ASC";
+	  break;
+	case 'newest':
+	  $orderby = "ORDER BY n.created DESC";
+	  break;
+	}
+	$keys = search_query_insert($keys, 'sort');
+      }
+
       // Build ranking expression (we try to map each parameter to a
       // uniform distribution in the range 0..1).
       $ranking = array();
@@ -906,7 +923,7 @@ function node_search($op = 'search', $ke
       $select2 = (count($ranking) ? implode(' + ', $ranking) : 'i.relevance') .' AS score';
 
       // Do search
-      $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. $join1 .' INNER JOIN {users} u ON n.uid = u.uid', $conditions1 . (empty($where1) ? '' : ' AND '. $where1), $arguments1, $select2, $join2, $arguments2);
+      $find = do_search($keys, 'node', 'INNER JOIN {node} n ON n.nid = i.sid '. $join1 .' INNER JOIN {users} u ON n.uid = u.uid', $conditions1 . (empty($where1) ? '' : ' AND '. $where1), $arguments1, $select2, $join2, $arguments2, $orderby);
 
       // Load results
       $results = array();
@@ -2525,6 +2542,18 @@ function node_form_alter($form_id, &$for
       '#suffix' => '</div>',
       '#options' => $types,
     );
+
+    // Sort order
+    $form['advanced']['sort'] = array
+      ('#type' => 'select',
+       '#title' => t('Sort results by'),
+       '#prefix' => '<div class="criterion">',
+       '#suffix' => '</div>',
+       '#options' => array('' => '', 'score' => 'Relevance',
+			   'newest' => 'Newest First',
+			   'oldest' => 'Oldest First'),
+       );
+
     $form['advanced']['submit'] = array(
       '#type' => 'submit',
       '#value' => t('Advanced search'),
@@ -2555,6 +2584,9 @@ function node_search_validate($form_id, 
   if (isset($form_values['category']) && is_array($form_values['category'])) {
     $keys = search_query_insert($keys, 'category', implode(',', $form_values['category']));
   }
+  if (isset($form_values['sort'])) {
+    $keys = search_query_insert($keys, 'sort', $form_values['sort']);
+  }
   if ($form_values['or'] != '') {
     if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' '. $form_values['or'], $matches)) {
       $keys .= ' '. implode(' OR ', $matches[1]);
