Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug or Feature? Sorting strings with quotes #76

Open
thiagowfx opened this issue Feb 27, 2025 · 2 comments
Open

Bug or Feature? Sorting strings with quotes #76

thiagowfx opened this issue Feb 27, 2025 · 2 comments

Comments

@thiagowfx
Copy link

Given main.yaml:

# keep-sorted start
- b
- "d"
- c
# keep-sorted end

What should happen? I would have expected "d" to come last:

% cat main.yaml
# keep-sorted start
- b
- c
- "d"
# keep-sorted end

This is what happens instead (v0.6.0):

% keep-sorted main.yaml
% cat main.yaml
# keep-sorted start
- "d"
- b
- c
# keep-sorted end

It's technically correct from a lexical sorting perspective, but is it intended?

Assuming it is intended, is there a workaround to make it consider ASCII characters only? Perhaps with by_regex?

@JeffFaer
Copy link
Collaborator

JeffFaer commented Feb 27, 2025

keep-sorted is language agnostic and doesn't really try to understand the syntax of file it's operating in. There's some support for general syntax with block=yes, but even that option wouldn't have changed how this got sorted.

is there a workaround to make it consider ASCII characters only?

Nit: " is an ASCII character ;)

Depending on whether by_regex works for you, this could be an interesting FR: Perhaps we should have an option that tells keep-sorted exactly which characters to consider while sorting.

Perhaps with by_regex?

Have you tried any by_regexs? Assuming these YAML strings are relatively simple (they all match the form - \w+ or - "\w+[\w ]*") I imagine something like by_regex=['\w+[\w ]*'] would work

@thiagowfx
Copy link
Author

Nit: " is an ASCII character ;)

aha, indeed, I meant alphanumeric :P

A couple of forms that work as per my original intent:

# keep-sorted start by_regex=\w+
- b
- c
- "d"
# keep-sorted end
# keep-sorted start by_regex=['\w+[\w ]*']
- b
- c
- "d"
# keep-sorted end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants