--- flexifilter.admin.inc	2010-07-27 19:24:33.000000000 +0200
+++ flexifilter.admin.inc.modified	2010-07-27 18:48:25.000000000 +0200
@@ -144,7 +144,7 @@
         $us['setting_'. $key] = $value;
       }
     }
-    $us['weight'] = $n;
+
     $us = array_merge($us, $component);
     unset($us['settings']);
     $id = $us['id'];
@@ -616,6 +616,7 @@
       $component = array(
         'class' => $data_us['class'],
         'settings' => array(),
+        'weight' => $data_us['weight'],
       );
       foreach ($data_us as $key => $value) {
         if (strncmp($key, 'setting_', 8) == 0) {
@@ -944,4 +945,4 @@
   $form_state['storage']['data'] = (variable_get('flexifilter_preview_text', array()));
   variable_del('flexifilter_preview');
   variable_del('flexifilter_preview_text');
-}
\ No newline at end of file
+}
--- flexifilter.install	2010-07-27 19:24:33.000000000 +0200
+++ flexifilter.install.modified	2010-07-27 19:23:08.000000000 +0200
@@ -134,6 +134,12 @@
         'not null' => TRUE,
         'description' => t('Serialized array of any settings that have been set for this part'),
       ),
+      'weight' => array(
+        'type' => 'int',
+        'unsigned' => FALSE,
+        'not null' => TRUE,
+        'description' => t('The component or condition\'s weight'),
+      ),
     ),
     'primary key' => array('pid'),
     'indexes' => array(
@@ -161,4 +167,18 @@
   );
   db_add_field($ret, $table, $field_name, $field_def);
   return $ret;
-}
\ No newline at end of file
+}
+
+function flexifilter_update_6103($ret)
+{
+  $table = 'flexifilters_parts';
+  $field_name = 'weight';
+  $field_def = array(
+    'type' => 'int',
+    'unsigned' => FALSE,
+    'not null' => TRUE,
+    'description' => t('The component or condition\'s weight'),
+  );
+  db_add_field($ret,$table,$field_name,$field_def);
+  return $ret;
+}
--- flexifilter.module	2010-07-27 19:24:33.000000000 +0200
+++ flexifilter.module.modified	2010-07-27 18:58:17.000000000 +0200
@@ -535,6 +535,7 @@
 }
 
 function _flexifilter_save_filter_components($components, $fid, &$pids_to_reuse, $parent = FALSE) {
+
   if ($parent === FALSE) {
     if ($reuse_pid = array_shift($pids_to_reuse)) {
       db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = 0, type = %d, class_name = '', settings = '' WHERE pid = %d",
@@ -558,12 +559,12 @@
         unset($child['settings']['condition']);
       }
       if ($child_cid = array_shift($pids_to_reuse)) {
-        db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
-          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']), $child_cid);
+        db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s', weight=%d WHERE pid = %d",
+          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']), $child['weight'], $child_cid);
       }
       else {
-        db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
-          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']));
+        db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings, weight) VALUES (%d, %d, %d, '%s', '%s', %d)",
+          $fid, $parent, FLEXIFILTER_PART_TYPE_COMPONENT, $child['class'], serialize($child['settings']), $child['weight']);
         $child_cid = db_last_insert_id('flexifilters_parts', 'pid');
       }
       if (isset($child_components)) {
@@ -583,12 +584,12 @@
     unset($condition['settings']['conditions']);
   }
   if ($our_pid = array_shift($pids_to_reuse)) {
-    db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s' WHERE pid = %d",
-      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $condition['class'], serialize($condition['settings']), $our_pid);
+    db_query("UPDATE {flexifilters_parts} SET fid = %d, parent_pid = %d, type = %d, class_name = '%s', settings = '%s', weight=%d WHERE pid = %d",
+      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $condition['class'], serialize($condition['settings']), $condition['weight'], $our_pid);
   }
   else {
-    db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings) VALUES (%d, %d, %d, '%s', '%s')",
-      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $condition['class'], serialize($condition['settings']));
+    db_query("INSERT INTO {flexifilters_parts} (fid, parent_pid, type, class_name, settings, weight) VALUES (%d, %d, %d, '%s', '%s', %d)",
+      $fid, $parent, FLEXIFILTER_PART_TYPE_CONDITION, $condition['class'], serialize($condition['settings']), $condition['weight']);
     $our_pid = db_last_insert_id('flexifilters_parts', 'pid');
   }
   if (isset($condition_conditions)) {
@@ -636,7 +637,7 @@
 
   if ($include_components) {
     // Fetch all the parts of the filter and store them in a flat array
-    $result             = db_query('SELECT * FROM {flexifilters_parts} WHERE fid = %d', $row->fid);
+    $result             = db_query('SELECT * FROM {flexifilters_parts} WHERE fid = %d ORDER BY weight ASC', $row->fid);
     $components_flat    = array();
     $component_children = array();
     $id_next            = 0;
@@ -646,6 +647,7 @@
         'settings' => $row->settings,
         'type' => $row->type,
         'pid' => $row->pid,
+        'weight' => $row->weight,
       );
       $id_next = max($id_next, $row->pid + 1);
       if (!isset($component_children[$row->parent_pid])) {
@@ -678,6 +680,7 @@
           'class' => $child['class'],
           'settings' => unserialize($child['settings']),
           'id' => $child['pid'],
+          'weight'=> $child['weight'],
         );
         if ($class['contains_components']) {
           $child_reconstructed['settings']['components'] = _flexifilter_reconstruct_components($child_pid, $components_flat, $component_children);
@@ -787,4 +790,4 @@
     }
   }
   return FALSE;
-}
\ No newline at end of file
+}
