keep-sorted icon indicating copy to clipboard operation
keep-sorted copied to clipboard

Bug or Feature? Sorting strings with quotes

Open thiagowfx opened this issue 11 months ago • 3 comments

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?

thiagowfx avatar Feb 27 '25 10:02 thiagowfx

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

JeffFaer avatar Feb 27 '25 15:02 JeffFaer

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

thiagowfx avatar Mar 03 '25 00:03 thiagowfx

Related:

    # keep-sorted start by_regex=['\w+[\w ]*']
    - azure-workload-identity
    - clustermon
    - flatfile
    - kube2iam
    - mongo
    - node-local-dns
    - "pgbouncer*"
    - "rabbitmq*"
    - "redis*"
    - traefik
    - vault
    # keep-sorted end

The above ("reference") is working as intended.

The following examples do not work as intended:

First example

    # keep-sorted start by_regex=[\w-]+
    - "pgbouncer*"
    - "rabbitmq*"
    - "redis*"
    - azure-workload-identity
    - clustermon
    - flatfile
    - kube2iam
    - mongo
    - node-local-dns
    - traefik
    - vault
    # keep-sorted end

Expected: The same as "reference".

Note that \w does not match ". I don't know why " is coming first.

Even if I replace it with by_regex=[a-z-]+, the result is the same.

Second example

    # keep-sorted start ignore_prefixes="
    - "pgbouncer*"
    - "rabbitmq*"
    - "redis*"
    - azure-workload-identity
    - clustermon
    - flatfile
    - kube2iam
    - mongo
    - node-local-dns
    - traefik
    - vault
    # keep-sorted end

Expected: The same as "reference".

Apparently the quotes are not ignored.

thiagowfx avatar Mar 13 '25 11:03 thiagowfx