Skip to content

Commit a76f339

Browse files
sort alkis search results
1 parent d0b4729 commit a76f339

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

packages/clients/dish/src/mapConfigurations/searchConfigParams.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,43 @@ import {
99

1010
const groupDenkmalsuche = 'groupDenkmalsuche'
1111

12+
/**
13+
* Sortiert Features nach mehreren Eigenschaften
14+
* @param features - Array von Features
15+
* @param sortKeys - Array von Property-Namen zum Sortieren (in Prioritätsreihenfolge)
16+
* @param numericKeys - Array von Property-Namen, die numerisch statt alphabetisch sortiert werden sollen
17+
* @returns Sortiertes Features-Array
18+
*/
19+
const sortFeaturesByProperties = (
20+
features: any[],
21+
sortKeys: string[],
22+
numericKeys: string[] = []
23+
): any[] => {
24+
return features.sort((a, b) => {
25+
for (const key of sortKeys) {
26+
const valueA = a.properties?.[key] ?? ''
27+
const valueB = b.properties?.[key] ?? ''
28+
29+
let comparison = 0
30+
31+
if (numericKeys.includes(key)) {
32+
// Numerische Sortierung
33+
const numA = parseFloat(String(valueA)) || 0
34+
const numB = parseFloat(String(valueB)) || 0
35+
comparison = numA - numB
36+
} else {
37+
// Alphabetische Sortierung
38+
comparison = String(valueA).localeCompare(String(valueB))
39+
}
40+
41+
if (comparison !== 0) {
42+
return comparison
43+
}
44+
}
45+
return 0
46+
})
47+
}
48+
1249
export const searchMethods = {
1350
denkmalsucheAutocomplete: {
1451
groupId: groupDenkmalsuche,
@@ -107,6 +144,23 @@ export const searchMethods = {
107144
'{{flstkennz}}',
108145
],
109146
},
147+
resultModifier: (featureCollection) => {
148+
if (
149+
featureCollection.features === undefined ||
150+
featureCollection.features === null
151+
) {
152+
return featureCollection
153+
}
154+
const featuresSorted = sortFeaturesByProperties(
155+
featureCollection.features,
156+
['gemarkung', 'flur', 'flstnrzae', 'flstnrnen'],
157+
['flur', 'flstnrzae', 'flstnrnen']
158+
)
159+
return {
160+
...featureCollection,
161+
features: featuresSorted,
162+
}
163+
},
110164
},
111165
}
112166

0 commit comments

Comments
 (0)