diff --git a/src/views/services/list/hooks/useServiceColumn.tsx b/src/views/services/list/hooks/useServiceColumn.tsx index 5156f52c..cd4b5652 100644 --- a/src/views/services/list/hooks/useServiceColumn.tsx +++ b/src/views/services/list/hooks/useServiceColumn.tsx @@ -7,6 +7,8 @@ import { useNetworkingTranslation } from '@utils/hooks/useNetworkingTranslation' import { ServiceWithHealth } from '@utils/types'; import { objectColumnSorting, sortByEndpointHealthStatus } from '@utils/utils/sorting'; +import { sortServicesByLocation } from '../utils/utils'; + export const tableColumnClasses = [ 'pf-v6-u-w-25-on-xl', 'pf-m-hidden pf-m-visible-on-md', @@ -60,7 +62,7 @@ const useServiceColumn = (): { id: string; title: string }[] => { { id: 'location', props: { className: tableColumnClasses[5] }, - sort: 'spec.clusterIP', + sort: (data, direction) => sortServicesByLocation(data, direction), title: t('Location'), transforms: [sortable], }, diff --git a/src/views/services/list/utils/utils.ts b/src/views/services/list/utils/utils.ts new file mode 100644 index 00000000..df23b503 --- /dev/null +++ b/src/views/services/list/utils/utils.ts @@ -0,0 +1,29 @@ +import { SortByDirection } from '@patternfly/react-table'; +import { ServiceWithHealth } from '@utils/types'; + +const getServiceLocationSortValue = (service: ServiceWithHealth): string => { + switch (service?.spec?.type) { + case 'LoadBalancer': { + const ingress = service?.status?.loadBalancer?.ingress?.[0]; + return ingress?.hostname || ingress?.ip || ''; + } + case 'ExternalName': + return service?.spec?.externalName || ''; + default: + return service?.spec?.clusterIP || ''; + } +}; + +export const sortServicesByLocation = (data: ServiceWithHealth[], direction: SortByDirection) => { + const compareFunction = (a: ServiceWithHealth, b: ServiceWithHealth) => { + const aValue = getServiceLocationSortValue(a); + const bValue = getServiceLocationSortValue(b); + + return ( + (direction === SortByDirection.asc ? 1 : -1) * + aValue.localeCompare(bValue, undefined, { numeric: true, sensitivity: 'base' }) + ); + }; + + return data.sort(compareFunction); +};