Skip to content

Commit 5133ebe

Browse files
committed
fix: sort filter keys alphabetically
1 parent 3ff761b commit 5133ebe

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@hyperdx/app': patch
3+
---
4+
5+
fix: keys inside Map and JSON filter groups (e.g. `LogAttributes`) in the search
6+
page filter sidebar are sorted alphabetically, after keys with a selected value
7+
and pinned keys.

‎packages/app/src/components/DBSearchPageFilters.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import { SharedFiltersSection } from './DBSearchPageFilters/SharedFilters';
8585
import {
8686
getFilterStateEntry,
8787
groupFacetsByBaseName,
88+
sortNestedFacetKeys,
8889
toQuotedClickHouseKeyExpression,
8990
} from './DBSearchPageFilters/utils';
9091

@@ -1340,6 +1341,9 @@ const DBSearchPageFiltersComponent = ({
13401341
});
13411342
}
13421343

1344+
// sort keys within nested groups alphabetically
1345+
sortNestedFacetKeys(_facets);
1346+
13431347
// prioritize facets that are primary keys
13441348
_facets.sort((a, b) => {
13451349
const aIsPk = isFieldPrimary(tableMetadata, a.key);

‎packages/app/src/components/DBSearchPageFilters/utils.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
cleanClickHouseExpression,
55
getFilterStateEntry,
66
groupFacetsByBaseName,
7+
sortNestedFacetKeys,
78
toClickHouseKeyExpression,
89
toQuotedClickHouseKeyExpression,
910
} from './utils';
@@ -77,6 +78,34 @@ describe('groupFacetsByBaseName — dot/bracket de-duplication', () => {
7778
});
7879
});
7980

81+
describe('sortNestedFacetKeys', () => {
82+
it('sorts keys A–Z within each nested group without reordering groups or other facets', () => {
83+
const facets = [
84+
'SeverityText',
85+
"ResourceAttributes['service.name']",
86+
"LogAttributes['user.id']",
87+
'ServiceName',
88+
"ResourceAttributes['host.name']",
89+
"LogAttributes['attr10']",
90+
'LogAttributes.http.method',
91+
"LogAttributes['attr9']",
92+
].map(key => ({ key }));
93+
94+
sortNestedFacetKeys(facets);
95+
96+
expect(facets.map(f => f.key)).toEqual([
97+
'SeverityText',
98+
'ServiceName',
99+
"ResourceAttributes['host.name']",
100+
"ResourceAttributes['service.name']",
101+
"LogAttributes['attr9']",
102+
"LogAttributes['attr10']",
103+
'LogAttributes.http.method',
104+
"LogAttributes['user.id']",
105+
]);
106+
});
107+
});
108+
80109
describe('getFilterStateEntry', () => {
81110
const makeEntry = (included: string[]): FilterState[string] => ({
82111
included: new Set<string | boolean>(included),

‎packages/app/src/components/DBSearchPageFilters/utils.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ export function groupFacetsByBaseName(
122122
return { grouped: Array.from(grouped.values()), nonGrouped };
123123
}
124124

125+
const naturalCompare = new Intl.Collator(undefined, { numeric: true }).compare;
126+
127+
// Sorts the keys inside each nested group A–Z, keeping groups in the order they
128+
// first appear. Other facets keep their order and move ahead of the nested
129+
// ones, which is harmless because grouped and non-grouped facets render
130+
// separately.
131+
export function sortNestedFacetKeys(facets: { key: string }[]) {
132+
const groupOrder = new Map<string, number>();
133+
for (const { key } of facets) {
134+
const baseName = parseMapFieldName(key)?.baseName;
135+
if (baseName != null && !groupOrder.has(baseName)) {
136+
groupOrder.set(baseName, groupOrder.size);
137+
}
138+
}
139+
facets.sort((a, b) => {
140+
const nestedA = parseMapFieldName(a.key);
141+
const nestedB = parseMapFieldName(b.key);
142+
if (!nestedA || !nestedB) return Number(!!nestedA) - Number(!!nestedB);
143+
return (
144+
groupOrder.get(nestedA.baseName)! - groupOrder.get(nestedB.baseName)! ||
145+
naturalCompare(nestedA.propertyPath, nestedB.propertyPath)
146+
);
147+
});
148+
}
149+
125150
// Look up a filterState entry by either bracket-form or dot-form map sub-key.
126151
// Bracket form is the canonical SQL key used in facet results; dot form is
127152
// what setFilterValue stores after its parseKeyPath().join('.') normalization

0 commit comments

Comments
 (0)