Skip to content

Commit 6c382ba

Browse files
extract sorting logic into separate file
1 parent ac7f30c commit 6c382ba

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

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

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,11 @@ import {
66
denkmaelerWFS,
77
alkisWfs,
88
} from '../servicesConstants'
9+
import { sortFeaturesByProperties } from '../utils/sortFeaturesByProperties'
910

1011
const groupDenkmalsuche = 'groupDenkmalsuche'
1112
export const categoryIdAlkisSearch = 'categoryIdAlkisSearch'
1213

13-
/**
14-
* Sortiert Features nach mehreren Eigenschaften
15-
* @param features - Array von Features
16-
* @param sortKeys - Array von Property-Namen zum Sortieren (in Prioritätsreihenfolge)
17-
* @param numericKeys - Array von Property-Namen, die numerisch statt alphabetisch sortiert werden sollen
18-
* @returns Sortiertes Features-Array
19-
*/
20-
const sortFeaturesByProperties = (
21-
features: any[],
22-
sortKeys: string[],
23-
numericKeys: string[] = []
24-
): any[] => {
25-
return features.sort((a, b) => {
26-
for (const key of sortKeys) {
27-
const valueA = a.properties?.[key] ?? ''
28-
const valueB = b.properties?.[key] ?? ''
29-
30-
let comparison = 0
31-
32-
if (numericKeys.includes(key)) {
33-
const numA = parseFloat(String(valueA)) || 0
34-
const numB = parseFloat(String(valueB)) || 0
35-
comparison = numA - numB
36-
} else {
37-
comparison = String(valueA).localeCompare(String(valueB))
38-
}
39-
40-
if (comparison !== 0) {
41-
return comparison
42-
}
43-
}
44-
return 0
45-
})
46-
}
47-
4814
export const searchMethods = {
4915
denkmalsucheAutocomplete: {
5016
groupId: groupDenkmalsuche,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export const sortFeaturesByProperties = (
2+
features: any[],
3+
sortKeys: string[],
4+
numericKeys: string[] = []
5+
): any[] => {
6+
return features.sort((a, b) => {
7+
for (const key of sortKeys) {
8+
const valueA = a.properties?.[key] ?? ''
9+
const valueB = b.properties?.[key] ?? ''
10+
11+
let comparison = 0
12+
13+
if (numericKeys.includes(key)) {
14+
const numA = parseFloat(String(valueA)) || 0
15+
const numB = parseFloat(String(valueB)) || 0
16+
comparison = numA - numB
17+
} else {
18+
comparison = String(valueA).localeCompare(String(valueB))
19+
}
20+
21+
if (comparison !== 0) {
22+
return comparison
23+
}
24+
}
25+
return 0
26+
})
27+
}

0 commit comments

Comments
 (0)