diff --git a/flag_weights.info b/flag_weights.info
index 684eb53..7c87af8 100644
--- a/flag_weights.info
+++ b/flag_weights.info
@@ -3,3 +3,4 @@ description = "Add flag-weights to the Flag module."
 package = Flags
 core = 7.x
 dependencies[] = flag
+files[] = views_handler_sort_flag_weights.inc
diff --git a/flag_weights.module b/flag_weights.module
index 4bb7e04..c3039ba 100644
--- a/flag_weights.module
+++ b/flag_weights.module
@@ -50,7 +50,7 @@ function flag_weights_views_data() {
       'click sortable' => TRUE,
     ),
     'sort' => array(
-      'handler' => 'views_handler_sort',
+      'handler' => 'views_handler_sort_flag_weights',
     ),
   );
 
diff --git a/views_handler_sort_flag_weights.inc b/views_handler_sort_flag_weights.inc
new file mode 100644
index 0000000..ec3e5cc
--- /dev/null
+++ b/views_handler_sort_flag_weights.inc
@@ -0,0 +1,40 @@
+<?php
+
+/**
+ * Handler that supports putting flagged content at the top or bottom
+ * (compared to unflagged content).
+ */
+class views_handler_sort_flag_weights extends views_handler_sort {
+  function option_definition() {
+    $options = parent::option_definition();
+
+    $options['flagorder'] = array('default' => 'NONE');
+
+    return $options;
+  }
+
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+
+    $options = array(
+      'NONE' => t('Default order'),
+      'ASC' => t('Flagged at top'),
+      'DESC' => t('Flagged at bottom'),
+    );
+
+    $form['flagorder'] = array(
+      '#title' => t('Ordering of Flagged Values'),
+      '#type' => 'radios',
+      '#options' => $options,
+      '#default_value' => $this->options['flagorder'],
+    );
+  }
+
+  function query() {
+    parent::query();
+    if ($this->options['flagorder'] != 'NONE') {
+      $this->ensure_my_table();
+      $this->query->add_orderby(NULL, $this->table_alias . '.' . $this->real_field . ' IS NULL', $this->options['flagorder']);
+    }
+  }
+}
