Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.

Commit 4b949dc

Browse files
committed
Fix sorting
1 parent 923c193 commit 4b949dc

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

components/search/resultList.tsx

+38-15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,41 @@ const ResultList = ({
8484
}
8585
}
8686

87+
const sortArrayByTeamAndName = (array: any[], owner: string, nameKey: string) => {
88+
if (array) {
89+
array.sort((a, b) => {
90+
// If owner is defined for both a and b
91+
if (a[owner] && b[owner]) {
92+
let comparison = a[owner].toLowerCase().localeCompare(b[owner].toLowerCase());
93+
// If owner is not the same, sort by owner
94+
if (comparison !== 0) return comparison;
95+
} else if (a[owner]) {
96+
// If owner is defined for a but not for b, a comes first
97+
return -1;
98+
} else if (b[owner]) {
99+
// If owner is defined for b but not for a, b comes first
100+
return 1;
101+
} else {
102+
// If both a and b are undefined, they are equal in terms of sorting
103+
return 0;
104+
}
105+
// If owner is the same or both are undefined, sort by name
106+
if (a[nameKey] && b[nameKey]) {
107+
return a[nameKey].toLowerCase().localeCompare(b[nameKey].toLowerCase());
108+
} else if (a[nameKey]) {
109+
// If name is defined for a but not for b, a comes first
110+
return -1;
111+
} else if (b[nameKey]) {
112+
// If name is defined for b but not for a, b comes first
113+
return 1;
114+
} else {
115+
// If both a and b are undefined, they are equal in terms of sorting
116+
return 0;
117+
}
118+
});
119+
}
120+
}
121+
87122
if (search && !!searchParam) {
88123
var { data, loading, error } = search
89124

@@ -96,21 +131,6 @@ const ResultList = ({
96131
(d) => !isDataProduct(d.result)
97132
)
98133

99-
const sortByTeamAndName = (a: any, b: any) => {
100-
if (a.teamkatalogenURL && b.teamkatalogenURL) {
101-
let comparison = a.teamkatalogenURL.localeCompare(b.teamkatalogenURL);
102-
if (comparison !== 0) return comparison;
103-
} else if (a.teamkatalogenURL) {
104-
return -1; // a comes first if b is undefined
105-
} else if (b.teamkatalogenURL) {
106-
return 1; // b comes first if a is undefined
107-
}
108-
// If teamkatalogenURL is the same or both are undefined, sort by name
109-
return a.name.localeCompare(b.name);
110-
}
111-
if (dataproducts) { dataproducts.sort(sortByTeamAndName) }
112-
if (stories) { stories.sort(sortByTeamAndName) }
113-
if (insightProducts) { insightProducts.sort(sortByTeamAndName) }
114134

115135
return (
116136
<Results>
@@ -178,6 +198,7 @@ const ResultList = ({
178198
)
179199
}
180200
if (dataproducts) {
201+
sortArrayByTeamAndName(dataproducts, 'owner.group', 'name')
181202
return (
182203
<Results>
183204
{dataproducts.map((d, idx) => (
@@ -195,6 +216,7 @@ const ResultList = ({
195216
}
196217

197218
if (stories) {
219+
sortArrayByTeamAndName(stories, 'group', 'name')
198220
return (
199221
<div>
200222
<Results>
@@ -222,6 +244,7 @@ const ResultList = ({
222244
}
223245

224246
if (insightProducts) {
247+
sortArrayByTeamAndName(insightProducts, 'group', 'name')
225248
return (
226249
<div>
227250
<Results>

0 commit comments

Comments
 (0)