diff --git a/apachesolr_sort.admin.inc b/apachesolr_sort.admin.inc
index c0772ef..71f1ee1 100644
--- a/apachesolr_sort.admin.inc
+++ b/apachesolr_sort.admin.inc
@@ -60,4 +60,48 @@ 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
+}
+
+function apachesolr_sort_grouping_settings() {
+  return drupal_get_form('apachesolr_sort_grouping_settings_form');
+}
+
+function apachesolr_sort_grouping_settings_form($form_state) {
+
+  $env_id = apachesolr_default_environment();
+  $environment = apachesolr_environment_load($env_id);
+
+  if($environment) {
+    $service = new DrupalApacheSolrService($environment['url'], $env_id);
+    $luke = $service->getLuke();
+    //kpr($luke);
+    $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 = array();
+  $form['grouping_field'] = array(
+    '#title' => t("Choose a field to group by"),
+    '#type' => 'select',
+    '#options' => $items,
+    '#default_value' => variable_get('apachesolr_sort_group_by_field', 'tos_name'),
+  );
+  $form['grouping_submit'] = array(
+    '#type' => 'submit',
+    '#value' => t("Submit"),
+  );
+
+  return $form;
+}
+
+function apachesolr_sort_grouping_settings_form_submit($form, &$form_state) {
+  variable_set('apachesolr_sort_group_by_field', $form_state['values']['grouping_field']);
+}
diff --git a/apachesolr_sort.info b/apachesolr_sort.info
index 79a8783..9cf3e72 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
\ No newline at end of file
diff --git a/apachesolr_sort.module b/apachesolr_sort.module
index b591a58..01edd60 100644
--- a/apachesolr_sort.module
+++ b/apachesolr_sort.module
@@ -14,6 +14,15 @@ function apachesolr_sort_menu() {
     'weight'             => -10,
     'type'               => MENU_LOCAL_TASK,
   );
+  $items['admin/config/search/apachesolr/grouping'] = array(
+    'title' => 'Grouping',
+    'description' => 'Manage ApacheSolr Grouping.',
+    'page callback' => 'apachesolr_sort_grouping_settings',
+    'access arguments'   => array('administer search'),
+    'file' => 'apachesolr_sort.admin.inc',
+    'weight'             => 0,
+    'type'               => MENU_LOCAL_TASK,
+  );
   return $items;
 }
 
@@ -38,6 +47,50 @@ function apachesolr_sort_apachesolr_query_prepare($query) {
   $query->setAvailableSorts($sorts);
 }
 
+/**
+ * Implements hook_apachesolr_query_alter
+ */
+function apachesolr_sort_apachesolr_query_alter(SolrBaseQuery $query) {
+  $env_id = $query->solr('getId');
+  $process_callback = apachesolr_environment_variable_get($env_id, 'process_response_callback', 'apachesolr_search_process_response');
+  if ($process_callback == "apachesolr_sort_process_response") {
+    $groupByField = variable_get('apachesolr_sort_group_by_field', 'tos_name');
+    $query->addParams(
+      array(
+        'group' => 'true',
+        'group.field' => $groupByField,
+        'group.format' => 'simple',
+        'group.limit' => '2',
+        'group.ngroups' => 'true',
+        'group.sort' => 'entity_id desc',
+        'group.facet' => 'true',
+      )
+    );
+  }
+}
+
+function apachesolr_sort_process_response($response, DrupalSolrQueryInterface $query) {
+  $groupByField = variable_get('apachesolr_sort_group_by_field', 'tos_name');
+  $total = $response->grouped->$groupByField->matches;
+  $grouped_results = array();
+  foreach ($response->grouped as $group_name => $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);
+    $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_weight_sort($a, $b) {
   return strcmp($a['weight'], $b['weight']);
 }
@@ -88,6 +141,36 @@ function apachesolr_sort_block_view($delta) {
 }
 
 /**
+ * Implements hook_form_FORM_ID_alter().
+ *
+ * Adds a "make multisite" option in the settings of any environment
+ */
+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;
+  $form['make_grouped'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Make this environment grouped-enable'),
+    '#default_value' => $is_grouped,
+  );
+  $form['actions']['save']['#submit'][] = 'apachesolr_sort_environment_edit_submit';
+}
+
+/**
+ * Submit callback for saving an environment to make it multisite capabe
+ */
+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');
+  }
+}
+
+/**
  * Displays the sort form as a dropdown.
  */
 function apachesolr_sort_sort_form_($form, &$form_state, SolrBaseQuery $query, array $sorts, array $solrsort) {
@@ -155,4 +238,4 @@ function apachesolr_sort_sort_form_submit($form, &$form_state) {
   }
   // Redirect to a URL with the solrsort params included.
   $form_state['redirect'] = array(current_path(), array('query' => $params));
-}
+}
\ No newline at end of file
