Skip to content

Commit

Permalink
use correct option property for variable options sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergej-Vlasov committed Dec 24, 2024
1 parent 60b8409 commit 626e057
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions packages/scenes/src/variables/variants/query/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,40 +74,11 @@ const getAllMatches = (str: string, regex: RegExp): RegExpExecArray[] => {
return results;
};

export const sortVariableValues = (options: any[], sortOrder: VariableSort) => {
export const sortVariableValues = (options: VariableValueOption[], sortOrder: VariableSort) => {
if (sortOrder === VariableSort.disabled) {
return options;
}

// @ts-ignore
const sortByNumeric = (opt) => {
if (!opt.text) {
return -1;
}
const matches = opt.text.match(/.*?(\d+).*/);
if (!matches || matches.length < 2) {
return -1;
} else {
return parseInt(matches[1], 10);
}
};

// @ts-ignore
const sortByNaturalSort = (options) => {
//@ts-ignore
return options.sort((a, b) => {
if (!a.text) {
return -1;
}

if (!b.text) {
return 1;
}

return a.text.localeCompare(b.text, undefined, { numeric: true });
});
};

switch (sortOrder) {
case VariableSort.alphabeticalAsc:
options = sortBy(options, 'label');
Expand Down Expand Up @@ -146,3 +117,29 @@ export const sortVariableValues = (options: any[], sortOrder: VariableSort) => {
}
return options;
};

function sortByNumeric(opt: VariableValueOption) {
if (!opt.label) {
return -1;
}
const matches = opt.label.match(/.*?(\d+).*/);
if (!matches || matches.length < 2) {
return -1;
} else {
return parseInt(matches[1], 10);
}
}

function sortByNaturalSort(options: VariableValueOption[]) {
return options.sort((a, b) => {
if (!a.label) {
return -1;
}

if (!b.label) {
return 1;
}

return a.label.localeCompare(b.label, undefined, { numeric: true });
});
}

0 comments on commit 626e057

Please sign in to comment.