diff --git a/modules/rooms_booking/includes/rooms_booking.availability_agent.inc b/modules/rooms_booking/includes/rooms_booking.availability_agent.inc
index 03c18eb..d3f01b1 100644
--- a/modules/rooms_booking/includes/rooms_booking.availability_agent.inc
+++ b/modules/rooms_booking/includes/rooms_booking.availability_agent.inc
@@ -304,8 +304,15 @@ class AvailabilityAgent {
 }
 
 function comparebyOptionals($a, $b) {
-  if (!isset($b['unit']->rooms_booking_unit_options[LANGUAGE_NONE]) && !isset($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE])) {
-    return 0;
+  // check if both have no options set, or (both do have options set and number of options is equal)
+  if ((!isset($b['unit']->rooms_booking_unit_options[LANGUAGE_NONE]) && 
+       !isset($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE]))
+      || ((isset($b['unit']->rooms_booking_unit_options[LANGUAGE_NONE]) && 
+           isset($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE])) &&
+            (count($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE]) == 
+                count($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE])))) {
+    // then sort by unit_id instead (no need to consider possibility of equal unit_id, since this is a unique primary key)
+    return $b['unit']->unit_id > $a['unit']->unit_id ? -1 : 1;
   }
 
   if (!isset($b['unit']->rooms_booking_unit_options[LANGUAGE_NONE]) && isset($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE])) {
@@ -318,3 +325,4 @@ function comparebyOptionals($a, $b) {
 
   return count($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE]) < count($a['unit']->rooms_booking_unit_options[LANGUAGE_NONE]) ? -1 : 1;
 }
+
