Index: /ttvd5/trunk/sites/all/modules/og/og_views.inc
===================================================================
--- /ttvd5/trunk/sites/all/modules/og/og_views.inc (revision 7201)
+++ /ttvd5/trunk/sites/all/modules/og/og_views.inc (revision 7294)
@@ -246,4 +246,9 @@
           'handler' => 'og_handler_sort_field_count',
           'help' => t('Sort by the number of Og subscribers'),
+        ),
+        'post_count' => array(
+          'name' => t('Og: Group: Post Count'),
+          'handler' => 'og_handler_sort_post_count',
+          'help' => t('Sort by the number of posts in a group'),
         ),
       ),
@@ -1311,4 +1316,21 @@
 }
 
+/**
+ * Views handler to sort by the number of posts
+ */
+function og_handler_sort_post_count($op, &$query, $sortinfo, $sort) {
+  if (method_exists($query, 'add_subquery')) {
+    $sql = "SELECT oa.group_nid, COUNT(*) as num FROM {og_ancestry} oa INNER JOIN {node} n ON n.nid = oa.nid WHERE n.status > 0 GROUP BY oa.group_nid";
+    $join = array(
+      'type' => 'inner',
+      'left' => array('table' => 'node', 'field' => 'nid'),
+      'right' => array('field' => 'group_nid'),
+    );
+    $query->add_subquery($sql, array(), $join, 'og_count');
+    $query->orderby[] = 'og_count.num '. $sort['sortorder'];
+  }
+}
+
+
 /*
  * Simple array for current group.
