 ui/ui.controller.inc | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 64 insertions(+), 1 deletion(-)

diff --git a/ui/ui.controller.inc b/ui/ui.controller.inc
index e502b0d..e2ad63d 100644
--- a/ui/ui.controller.inc
+++ b/ui/ui.controller.inc
@@ -181,6 +181,12 @@ class RulesUIController {
    *   - 'base path': Optionally, a different base path to use instead of the
    *     currently set RulesPluginUI::$basePath. If no base path has been set
    *     yet, the current path is used by default.
+   *   - 'sort': Provide a key by which to sort table rows. You may specify
+   *     multiple sort keys separated by a comma. Valid keys are:
+   *       "weight"
+   *       "event" (the label of the first event of a rule)
+   *       "label"
+   *       "id"
    *
    * @return Array
    *   A renderable array.
@@ -191,6 +197,7 @@ class RulesUIController {
       'show plugin' => TRUE,
       'show events' => isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule',
       'show execution op' => !(isset($conditions['plugin']) && $conditions['plugin'] == 'reaction rule'),
+      'sort' => 'label,weight,event',
     );
     // By default show only configurations owned by rules.
     $conditions += array(
@@ -205,7 +212,22 @@ class RulesUIController {
     }
 
     $entities = entity_load('rules_config', FALSE, $conditions);
-    ksort($entities);
+
+    $sort_options = explode(',', $options['sort']);
+    foreach ($sort_options as $sort) {
+      if ($sort == 'id') {
+        ksort($entities);
+      }
+      elseif ($sort == 'label') {
+        uasort($entities, array('self', 'sortObjectsByLabel'));
+      }
+      elseif ($sort == 'weight') {
+        uasort($entities, array('self', 'sortObjectsByWeight'));
+      }
+      elseif ($sort == 'event' && $options['show events']) {
+        uasort($entities, array('self', 'sortObjectsByEvent'));
+      }
+    }
 
     // Prepare some variables used by overviewTableRow().
     $this->event_info = rules_fetch_data('event_info');
@@ -246,6 +268,47 @@ class RulesUIController {
   }
 
   /**
+   * Helper function for sorting Rules Overview table rows by rule event.
+   */
+  public static function sortObjectsByEvent($a, $b) {
+    static $events_list;
+    if (!isset($events_list)) {
+      $events_list = rules_fetch_data('event_info');
+    }
+
+    // Get the first event and use its label for sorting.
+    $a_event = is_object($a) ? $a->events() : '';
+    $a_event = is_array($a_event) && isset($events_list[$a_event[0]]) ? $events_list[$a_event[0]]['label'] : '';
+    $b_event = is_object($b) ? $b->events() : '';
+    $b_event = is_array($b_event) && isset($events_list[$b_event[0]]) ? $events_list[$b_event[0]]['label'] : '';
+    if ($a_event == $b_event) {
+      return 0;
+    }
+    return ($a_event < $b_event) ? -1 : 1;
+  }
+
+  /**
+   * Helper function for sorting Rules Overview table rows by rule label.
+   */
+  public static function sortObjectsByLabel($a, $b) {
+    $a_label = (is_object($a) && isset($a->label)) ? $a->label : '';
+    $b_label = (is_object($b) && isset($b->label)) ? $b->label : '';
+    return strcmp($a_label, $b_label);
+  }
+
+  /**
+   * Helper function for sorting Rules Overview table rows by rule weight.
+   */
+  public static function sortObjectsByWeight($a, $b) {
+    $a_weight = (is_object($a) && isset($a->weight)) ? $a->weight : 0;
+    $b_weight = (is_object($b) && isset($b->weight)) ? $b->weight : 0;
+    if ($a_weight == $b_weight) {
+      return 0;
+    }
+    return ($a_weight < $b_weight) ? -1 : 1;
+  }
+
+  /**
    * Generates the row for a single rules config.
    *
    * @param $additional_cols
