Skip to content

Commit 5edbdb7

Browse files
Loki: Fix label browser not sorted after selection of a label (#107394)
* Explore(Loki): fix label browser not sorted after selection of a label Signed-off-by: Paulo Dias <[email protected]> * fix: use collator instead of localCompares Signed-off-by: Paulo Dias <[email protected]> --------- Signed-off-by: Paulo Dias <[email protected]> Co-authored-by: Zoltán Bedi <[email protected]>
1 parent fe3d069 commit 5edbdb7

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

public/app/plugins/datasource/loki/components/LokiLabelBrowser.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const MAX_LABEL_COUNT = 1000;
2626
const MAX_VALUE_COUNT = 10000;
2727
const MAX_AUTO_SELECT = 4;
2828
const EMPTY_SELECTOR = '{}';
29+
const collator = new Intl.Collator('en', { sensitivity: 'accent' });
2930

3031
export interface BrowserProps {
3132
languageProvider: LokiLanguageProvider;
@@ -68,7 +69,10 @@ export function buildSelector(labels: SelectableLabel[]): string {
6869
const selectedLabels = [];
6970
for (const label of labels) {
7071
if (label.selected && label.values && label.values.length > 0) {
71-
const selectedValues = label.values.filter((value) => value.selected).map((value) => value.name);
72+
const selectedValues = label.values
73+
.filter((value) => value.selected)
74+
.map((value) => value.name)
75+
.sort(collator.compare); // sort selected values alphabetically
7276
if (selectedValues.length > 1) {
7377
selectedLabels.push(`${label.name}=~"${selectedValues.map(escapeLabelValueInRegexSelector).join('|')}"`);
7478
} else if (selectedValues.length === 1) {
@@ -97,7 +101,13 @@ export function facetLabels(
97101
label.values?.filter((value) => value.selected).map((value) => value.name) || []
98102
);
99103
// Values for this label have not been requested yet, let's use the facetted ones as the initial values
100-
existingValues = possibleValues.map((value) => ({ name: value, selected: selectedValues.has(value) }));
104+
existingValues = possibleValues
105+
.slice()
106+
.sort(collator.compare) // sort raw label values alphabetically
107+
.map((value) => ({
108+
name: value,
109+
selected: selectedValues.has(value),
110+
}));
101111
}
102112
return { ...label, loading: false, values: existingValues, facets: existingValues.length };
103113
}

0 commit comments

Comments
 (0)