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

Add sort_keys option #48

Closed
cbw512 opened this issue Nov 11, 2023 · 1 comment
Closed

Add sort_keys option #48

cbw512 opened this issue Nov 11, 2023 · 1 comment

Comments

@cbw512
Copy link

cbw512 commented Nov 11, 2023

As a prettier option and a big improval for tests that compare a toml file, I think a sort_keys like json option is useful.It ensure two toml file with same content will be able to do a text-compare after sorting keys.

@hukkin
Copy link
Owner

hukkin commented Jan 3, 2024

Thanks for the issue!

As the README file mentions, Tomli-W respects sort order of the input dict, so you can do

import tomli_w

def order_dict(d):
    return {k: order_dict(v) if isinstance(v, dict) else v for k, v in sorted(d.items())}

d = {"b": 2, "a": {"b": 2, "a": 1}}
sorted_toml = tomli_w.dumps(order_dict(d))

It's trivial enough to come up with a sort function of your own that I'm not sure Tomli-W needs to ship with one (that may not sort exactly the way you want).

@cbw512 cbw512 closed this as completed Jan 5, 2024
@cbw512 cbw512 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2024
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