diff --git a/matrix.module b/matrix.module
index 02ebbcf..4ba8801 100644
--- a/matrix.module
+++ b/matrix.module
@@ -702,14 +702,21 @@ function matrix_field_formatter_view($entity_type, $entity, $field, $instance, $
         $element[0] = array(
           '#markup' => field_filter_xss($raw_items[0]['value']),
         );
-      }
+      
 
       //transpose the $items array into a [$row][$col] = value arangement
       foreach ($raw_items as $delta => $item) {
         $items[$item['row']][$item['col']][] = matrix_cell_value($item, $field);
       }
 
-      //work out how many rows and columns to show, which is the number of rows with at least some data in the row.
+      ksort($items);
+      foreach ($items as $row_id => $row) {
+        ksort($items[$row_id]);
+      }
+
+      // work out how many rows and columns to show, which is the number of rows
+      // with at least some data in the row.
+      // TODO this logic is wrong.
       foreach ($items as $row_id => $row) {
         foreach ($row as $col_id => $value) {
           if ($value != '') {
@@ -718,6 +725,7 @@ function matrix_field_formatter_view($entity_type, $entity, $field, $instance, $
         }
       }
 
+      // TODO this is inconsistent with the row count logic.
       if ($field['settings']['cols_count'] > 0) {
         $cols_count = $field['settings']['cols_count'];
       }
