diff --git a/ui/include/classes/screens/CScreenDiscovery.php b/ui/include/classes/screens/CScreenDiscovery.php
index 437a38c26ed..655ee13e7a0 100644
--- a/ui/include/classes/screens/CScreenDiscovery.php
+++ b/ui/include/classes/screens/CScreenDiscovery.php
@@ -140,9 +140,8 @@ class CScreenDiscovery extends CScreenBase {
 					$htime = $dhost['lastup'];
 				}
 
-				// $primary_ip stores the primary host ip of the dhost
-				$primary_ip = '';
-
+				// Collect all service data for this dhost
+				$dhost_services = [];
 				foreach ($dhosts[$dhost['dhostid']]['dservices'] as $dservice) {
 					$dservice = $dservices[$dservice['dserviceid']];
 					$host_name = '';
@@ -156,18 +155,37 @@ class CScreenDiscovery extends CScreenBase {
 						$host_status = $host['status'];
 					}
 
-					if ($primary_ip !== '') {
-						if ($primary_ip === $dservice['ip']) {
-							$htype = 'primary';
-						}
-						else {
-							$htype = 'slave';
-						}
-					}
-					else {
-						$primary_ip = $dservice['ip'];
-						$htype = 'primary';
+					$dhost_services[] = [
+						'dservice' => $dservice,
+						'host_name' => $host_name,
+						'hostid' => $hostid,
+						'host_status' => $host_status
+					];
+				}
+
+				// Sort by IP before determining primary
+				usort($dhost_services, function($a, $b) {
+					return inet_pton($a['dservice']['ip']) <=> inet_pton($b['dservice']['ip']);
+				});
+
+				$primary_ip = null;
+				foreach ($dhost_services as $entry) {
+					if ($entry['hostid'] !== '') {
+						$primary_ip = $entry['dservice']['ip'];
+						break;
 					}
+				}
+				if ($primary_ip === null && $dhost_services) {
+					$primary_ip = $dhost_services[0]['dservice']['ip'];
+				}
+
+				foreach ($dhost_services as $entry) {
+					$dservice = $entry['dservice'];
+					$host_name = $entry['host_name'];
+					$hostid = $entry['hostid'];
+					$host_status = $entry['host_status'];
+
+					$htype = ($dservice['ip'] === $primary_ip) ? 'primary' : 'slave';
 
 					if (!array_key_exists($dservice['ip'], $discovery_info)) {
 						$discovery_info[$dservice['ip']] = [
@@ -178,6 +196,7 @@ class CScreenDiscovery extends CScreenBase {
 							'host' => $host_name,
 							'status' => $host_status,
 							'hostid' => $hostid,
+							'dhostid' => $dhost['dhostid'],
 							'time' => $htime
 						];
 					}
@@ -229,7 +248,23 @@ class CScreenDiscovery extends CScreenBase {
 
 				$table->addRow($col);
 			}
-			order_result($discovery_info, $sort_field, $sort_order);
+
+			// Sort discovery_info: group slaves under their primary, primaries ordered by IP
+			$sorted_discovery_info = [];
+			$primaries = array_filter($discovery_info, fn($h) => $h['type'] === 'primary');
+			uasort($primaries, fn($a, $b) => inet_pton($a['ip']) <=> inet_pton($b['ip']));
+
+			foreach ($primaries as $primary_ip => $primary) {
+				$sorted_discovery_info[$primary_ip] = $primary;
+
+				foreach ($discovery_info as $ip => $h_data) {
+					if ($h_data['type'] === 'slave' && $h_data['dhostid'] === $primary['dhostid']) {
+						$sorted_discovery_info[$ip] = $h_data;
+						unset($sorted_discovery_info[$ip]['dhostid']);
+					}
+				}
+			}
+			$discovery_info = $sorted_discovery_info;
 
 			foreach ($discovery_info as $ip => $h_data) {
 				$dns = ($h_data['dns'] === '') ? '' : ' ('.$h_data['dns'].')';
@@ -248,7 +283,7 @@ class CScreenDiscovery extends CScreenBase {
 				$row = [
 					($h_data['type'] === 'primary')
 						? (new CSpan($ip.$dns))->addClass($h_data['class'])
-						: new CSpan([NBSP(), NBSP(), $ip.$dns]),
+						: (new CSpan([NBSP(), NBSP(), NBSP(), NBSP(), $ip.$dns]))->addClass($h_data['class']),
 					new CSpan($host),
 					(new CSpan((($h_data['time'] == 0 || $h_data['type'] === 'slave')
 						? ''
