diff --git a/apachesolr_sort.admin.inc b/apachesolr_sort.admin.inc
index c0772ef..575ea48 100644
--- a/apachesolr_sort.admin.inc
+++ b/apachesolr_sort.admin.inc
@@ -60,4 +60,74 @@ function apachesolr_sort_page_form_submit($form, &$form_state) {
       variable_set('apachesolr_sort_sort_weight_' . $key, $field['apachesolr_sort_sort_weight_' . $key]);
     }
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Displays the sort form as a dropdown.
+ */
+function apachesolr_sort_sort_form_($form, &$form_state, SolrBaseQuery $query, array $sorts, array $solrsort) {
+  $toggle = array('asc' => 'desc', 'desc' => 'asc');
+
+  $form['apachesolr_sort_query'] = array(
+    '#type' => 'value',
+    '#value' => $query,
+  );
+
+  // Build sort options.
+  $sort_options = array();
+  foreach ($sorts as $name => $data) {
+    $sort_options[$name] = check_plain($data['title']);
+  }
+
+  $form['apachesolr_sort_name'] = array(
+    '#type' => 'select',
+    '#title' => t('Field'),
+    '#options' => $sort_options,
+    '#default_value' => $solrsort['#name'],
+  );
+
+  $form['apachesolr_sort_direction'] = array(
+    '#type' => 'select',
+    '#title' => t('Direction'),
+    '#options' => array(
+      'asc' => t('Ascending'),
+      'desc' => t('Descending'),
+    ),
+    '#default_value' => $solrsort['#direction'],
+  );
+
+  $form['actions'] = array(
+    '#type' => 'actions',
+    '#weight' => 20,
+  );
+
+  $form['actions']['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Sort results'),
+  );
+
+  $form['#submit'][] = 'apachesolr_sort_sort_form_submit';
+
+  return $form;
+}
+
+/**
+ * Form submission handler for apachesolr_sort_apachesolr_sort_form().
+ */
+function apachesolr_sort_sort_form_submit($form, &$form_state) {
+  $query = $form_state['values']['apachesolr_sort_query'];
+  $name = $form_state['values']['apachesolr_sort_name'];
+  $direction = $form_state['values']['apachesolr_sort_direction'];
+
+  // Sets the Solr sort, gets query string parameters.
+  $query->setSolrsort($name, $direction);
+  $params = array_merge($_GET, $query->getSolrsortUrlQuery());
+  $params = drupal_get_query_parameters($params, array('q', 'page'));
+
+  // For sone reason query->getSolrsortUrlQuery() doesn't seem to handle score.
+  if ('score' == $name) {
+    $params = array();
+  }
+  // Redirect to a URL with the solrsort params included.
+  $form_state['redirect'] = array(current_path(), array('query' => $params));
+}
diff --git a/apachesolr_sort.info b/apachesolr_sort.info
index 79a8783..e2b029d 100644
--- a/apachesolr_sort.info
+++ b/apachesolr_sort.info
@@ -3,3 +3,10 @@ name=Apachesolr sort
 description=Manage your Apachesolr sorts through an UI
 package = Search Toolkit
 core = "7.x"
+
+files[] = apachesolr_sort.module
+files[] = apachesolr_sort.admin.inc
+scripts[] = apachesolr_sort.js
+
+dependencies[] = apachesolr
+dependencies[] = apachesolr_search
diff --git a/apachesolr_sort.js b/apachesolr_sort.js
new file mode 100644
index 0000000..4a291db
--- /dev/null
+++ b/apachesolr_sort.js
@@ -0,0 +1,17 @@
+jQuery(document).ready(function() {
+  var groupClasses = new Array();
+  jQuery('.search-result.solr-grouped').each(function(index, item){
+    item = jQuery(item)
+    currentGroupClass = item.attr('class').substr(item.attr('class').lastIndexOf('solr-group-'));
+    if(jQuery.inArray(currentGroupClass, groupClasses) < 0) {
+      groupClasses.push(currentGroupClass);
+    }
+  });
+
+  jQuery.each(groupClasses, function(index, item) {
+    currentGroup = jQuery('.search-result.solr-grouped.' + item);
+    currentGroup.wrapAll('<li id="' + item + '-all" />');
+    currentGroup.wrapAll('<ol class="apachesolr_search-results-grouped search-results-grouped">');
+    jQuery('#' + item + '-all').prepend('<span>Group: ' + item.replace('solr-group-', '') +'</span>');
+  });
+});
diff --git a/apachesolr_sort.module b/apachesolr_sort.module
index b591a58..d7f0187 100644
--- a/apachesolr_sort.module
+++ b/apachesolr_sort.module
@@ -11,7 +11,7 @@ function apachesolr_sort_menu() {
     'page arguments'     => array('apachesolr_sort_page_form'),
     'access arguments'   => array('administer site configuration'),
     'file'               => 'apachesolr_sort.admin.inc',
-    'weight'             => -10,
+    'weight'             => 10,
     'type'               => MENU_LOCAL_TASK,
   );
   return $items;
@@ -38,6 +38,68 @@ function apachesolr_sort_apachesolr_query_prepare($query) {
   $query->setAvailableSorts($sorts);
 }
 
+/**
+ * Implements hook_apachesolr_query_alter
+ */
+function apachesolr_sort_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
+  $env_id = $query->solr('getId');
+  $process_callback = apachesolr_environment_variable_get($env_id, 'process_response_callback', 'apachesolr_search_process_response');
+  $group_field = apachesolr_environment_variable_get($env_id, 'group_field', 'tos_name');
+  $group_limit = apachesolr_environment_variable_get($env_id, 'group_limit', '2');
+  if ($process_callback == "apachesolr_sort_process_response") {
+    $query->addParams(
+      array(
+        'group' => 'true',
+        'group.field' => $group_field,
+        'group.limit' => $group_limit,
+        'group.ngroups' => 'true',
+        'group.sort' => 'entity_id desc',
+        'group.facet' => 'true',
+      )
+    );
+    // start params are different. You should move similar to the group.limit amount
+    if (isset($query->page)) {
+      $query->removeParam('start');
+    }
+    if (isset($query->page)) {
+      $query->addParam('group.offset', $query->page * $group_limit);
+    }
+  }
+}
+
+function apachesolr_sort_process_response($response, DrupalSolrQueryInterface $query) {
+  $env_id = $query->solr('getId');
+  $group_field = apachesolr_environment_variable_get($env_id, 'group_field', 'tos_name');
+  $total = $response->grouped->$group_field->matches;
+  $grouped_results = array();
+  foreach ($response->grouped->$group_field->groups as $groupId => $grouped_response) {
+    $response_individual = new stdClass();
+    $response_individual->response = $grouped_response->doclist;
+    $response_individual->highlighting = $response->highlighting;
+    $results = apachesolr_search_process_response($response_individual, $query);
+    foreach ($results as &$result) {
+      $result['fields']['groupId'] = $groupId;
+      $result['fields']['groupValue'] = $grouped_response->groupValue;
+    }
+    $grouped_results = array_merge($grouped_results, $results);
+  }
+  
+  $searcher = $query->getSearcher();
+  $response->response = new stdClass();
+  $response->response->numFound = $total;
+  // The response is cached so that it is accessible to the blocks and anything
+  // else that needs it beyond the initial search.
+  apachesolr_static_response_cache($searcher, $response);
+
+  return $grouped_results;
+}
+
+function apachesolr_sort_preprocess_search_result(&$variables) {
+  $variables['classes_array'][] = "solr-grouped";
+  $identifier = drupal_clean_css_identifier($variables['result']['fields']['groupValue']);
+  $variables['classes_array'][] = "solr-group-".$identifier;
+}
+
 function apachesolr_sort_weight_sort($a, $b) {
   return strcmp($a['weight'], $b['weight']);
 }
@@ -73,6 +135,7 @@ function apachesolr_sort_block_view($delta) {
         }
 
         // Gets apachesolr_sort_apachesolr_sort_form() arguments.
+        module_load_include('inc', 'apachesolr_sort', 'apachesolr_sort.admin');
         $new_query = clone $query;
         $sorts = $query->getAvailableSorts();
         $solrsort = $query->getSolrsort();
@@ -88,71 +151,66 @@ function apachesolr_sort_block_view($delta) {
 }
 
 /**
- * Displays the sort form as a dropdown.
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * Adds a "make multisite" option in the settings of any environment
  */
-function apachesolr_sort_sort_form_($form, &$form_state, SolrBaseQuery $query, array $sorts, array $solrsort) {
-  $toggle = array('asc' => 'desc', 'desc' => 'asc');
-
-  $form['apachesolr_sort_query'] = array(
-    '#type' => 'value',
-    '#value' => $query,
+function apachesolr_sort_form_apachesolr_environment_edit_form_alter(&$form, &$form_state, $form_id) {
+  $environment = reset($form_state['build_info']['args']);
+  $process_callback = apachesolr_environment_variable_get($environment['env_id'], 'process_response_callback', 'apachesolr_search_process_response');
+  $is_grouped = ($process_callback == 'apachesolr_search_process_response') ? FALSE : TRUE;
+  $group_field = apachesolr_environment_variable_get($environment['env_id'], 'group_field', 'tos_name');
+  $group_limit = apachesolr_environment_variable_get($environment['env_id'], 'group_limit', '2');
+  $form['make_grouped'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Make this environment grouped-enable'),
+    '#default_value' => $is_grouped,
   );
-
-  // Build sort options.
-  $sort_options = array();
-  foreach ($sorts as $name => $data) {
-    $sort_options[$name] = check_plain($data['title']);
-  }
-
-  $form['apachesolr_sort_name'] = array(
-    '#type' => 'select',
-    '#title' => t('Field'),
-    '#options' => $sort_options,
-    '#default_value' => $solrsort['#name'],
+  $form['group_limit'] = array(
+    '#type' => 'textfield',
+    '#title' => t('How many results per group'),
+    '#default_value' => $group_limit,
+    '#dependency' => array(
+      'edit-make-grouped' => array(1),
+    ),
   );
-
-  $form['apachesolr_sort_direction'] = array(
+  $service = new DrupalApacheSolrService($environment['url'], $environment['env_id']);
+  $luke = $service->getLuke();
+  $fields = (array)$luke->fields;
+  $items = array();
+  foreach ($fields as $key => $field) {
+    if (!in_array($field->type, array('string', 'text', 'sortString'))) {
+      continue;
+    }
+    $items[$key] = $key;
+  }
+  $form['group_field'] = array(
+    '#title' => t("Choose a field to group by"),
     '#type' => 'select',
-    '#title' => t('Direction'),
-    '#options' => array(
-      'asc' => t('Ascending'),
-      'desc' => t('Descending'),
+    '#options' => $items,
+    '#default_value' => $group_field,
+    '#dependency' => array(
+      'edit-make-grouped' => array(1),
     ),
-    '#default_value' => $solrsort['#direction'],
-  );
-
-  $form['actions'] = array(
-    '#type' => 'actions',
-    '#weight' => 20,
-  );
-
-  $form['actions']['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Sort results'),
   );
-
-  $form['#submit'][] = 'apachesolr_sort_sort_form_submit';
-
-  return $form;
+  $form['actions']['save']['#submit'][] = 'apachesolr_sort_environment_edit_submit';
 }
 
 /**
- * Form submission handler for apachesolr_sort_apachesolr_sort_form().
+ * Submit callback for saving an environment to make it multisite capabe
  */
-function apachesolr_sort_sort_form_submit($form, &$form_state) {
-  $query = $form_state['values']['apachesolr_sort_query'];
-  $name = $form_state['values']['apachesolr_sort_name'];
-  $direction = $form_state['values']['apachesolr_sort_direction'];
-
-  // Sets the Solr sort, gets query string parameters.
-  $query->setSolrsort($name, $direction);
-  $params = array_merge($_GET, $query->getSolrsortUrlQuery());
-  $params = drupal_get_query_parameters($params, array('q', 'page'));
-
-  // For sone reason query->getSolrsortUrlQuery() doesn't seem to handle score.
-  if ('score' == $name) {
-    $params = array();
+function apachesolr_sort_environment_edit_submit($form, &$form_state) {
+  // Enable or disable multisite
+  if ($form_state['values']['make_grouped']) {
+    apachesolr_environment_variable_set($form_state['values']['env_id'], 'process_response_callback', 'apachesolr_sort_process_response');
+  }
+  else {
+    apachesolr_environment_variable_del($form_state['values']['env_id'], 'process_response_callback');
+  }
+  if ($form_state['values']['group_field']) {
+    apachesolr_environment_variable_set($form_state['values']['env_id'], 'group_field', $form_state['values']['group_field']);
+  }
+  if ($form_state['values']['group_limit']) {
+    apachesolr_environment_variable_set($form_state['values']['env_id'], 'group_limit', $form_state['values']['group_limit']);
   }
-  // Redirect to a URL with the solrsort params included.
-  $form_state['redirect'] = array(current_path(), array('query' => $params));
 }
