Skip to content

Conversation

axelavargas
Copy link
Member

In core grafana we received a community contribution to implement natural sort. grafana/grafana#78024

This PR just adds their changes and a few unit tests.

@axelavargas axelavargas added minor Increment the minor version when merged area/variables Issues related to variables system in scenes labels Nov 14, 2023
@axelavargas axelavargas self-assigned this Nov 14, 2023
Comment on lines 104 to 116
} else if (sortType === 4) {
// Sort by natural sort
options.sort((a, b) => {
if (!a.text) {
return -1;
}

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const sortType = Math.ceil(sortOrder / 2);

and these if statements are pure cryptic gold, can we switch to using the enum VariableSort (and switch/case statement)? (you will have to add a //@ts-expect-error for the newly added enum that, add a TODO for that so we can find it and remove the ts-expect-error when we update the schema package in the future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that, it's confusing , I will do the changes 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@torkelo I did the refactor, but I couldn't find a way to avoid hardcoding the value of the new options. Also, there is some //ts-ignore around because options is any[]

@axelavargas axelavargas requested a review from torkelo November 14, 2023 11:04
Copy link
Collaborator

@torkelo torkelo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Do much easier to read and understand now!!

@axelavargas axelavargas merged commit 7eec507 into main Nov 14, 2023
@axelavargas axelavargas deleted the axelav/port-natural-sort branch November 14, 2023 12:56
@grafanabot
Copy link
Contributor

🚀 PR was released in v1.23.0 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/variables Issues related to variables system in scenes minor Increment the minor version when merged released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants