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

--no-sort option is unstable #102

Open
slafs opened this issue Jun 29, 2022 · 5 comments
Open

--no-sort option is unstable #102

slafs opened this issue Jun 29, 2022 · 5 comments

Comments

@slafs
Copy link

slafs commented Jun 29, 2022

For a simple JSON doc

{"z": "first", "a": "last"}

gron --no-sort keeps producing wrong results at around 12% rate.

Tried it with:

$ for i in $( seq 1000 ); do echo '{"z": "first", "a": "last"}' | gron --no-sort | column -x; done | sort | uniq -c
 125 json = {};		json.a = "last";	json.z = "first";
 875 json = {};		json.z = "first";	json.a = "last";

Tried it with:

$ gron --version
gron version dev
$ brew info gron
gron: stable 0.7.1 (bottled), HEAD
Make JSON greppable
https://github.com/tomnomnom/gron
/usr/local/Cellar/gron/0.7.1 (4 files, 5MB) *
  Poured from bottle on 2022-06-29 at 13:56:51
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/gron.rb
License: MIT
==> Dependencies
Build: go ✘
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 878 (30 days), 2,899 (90 days), 5,560 (365 days)
install-on-request: 876 (30 days), 2,899 (90 days), 5,561 (365 days)
build-error: 0 (30 days)

$ brew list gron
/usr/local/Cellar/gron/0.7.1/bin/gron
@slafs
Copy link
Author

slafs commented Jun 29, 2022

BTW this tool is fantastic and helps me a lot! Thanks for creating it :)

@RossPatterson
Copy link

--no-sort tells gron not to sort the output. It doesn't guarantee a stable order - if you want that, let it sort. The instability comes from the underlying Go map object, which randomizes string hashing functions.

@slafs
Copy link
Author

slafs commented Sep 19, 2022

Well, I guessed that might be the cause, but the underlying implementation shouldn't matter, right? The option is confusing when it works this way. What's the use of a non-sorted output if the input is being modified in the first place 😅?

@adamritter
Copy link

Hi,
I just tried with the tool I wrote ( https://github.com/adamritter/fastgron ), it preserves the order with gron (I'm not sure about ungron though yet :( )

for i in $( seq 1000 ); do echo '{"z": "first", "a": "last"}' | gron --no-sort | column -x; done | sort | uniq -c;

130 json = {}; json.a = "last"; json.z = "first";
870 json = {}; json.z = "first"; json.a = "last";
(base) ➜ ~/Documents/GitHub/fastgron/fastgron git:(main) for i in $( seq 1000 ); do echo '{"z": "first", "a": "last"}' | fastgron --no-sort | column -x; done | sort | uniq -c;

1000 json = {} json.z = "first" json.a = "last"

@dhimmel
Copy link

dhimmel commented Jan 12, 2024

+1 to --no-sort preserving input order if that's not to difficult to implement. Right now there is no way to retain order, but order often is often explicitly set in JSON objects to aid in interpretation / human-legibility.

dhimmel added a commit to dhimmel/dump-actions-context that referenced this issue Jan 12, 2024
--no-sort does not always preserve input order, hence it's better
to sort for deterministic output

refs tomnomnom/gron#102
dhimmel added a commit to dhimmel/dump-actions-context that referenced this issue Jan 12, 2024
--no-sort does not always preserve input order, hence it's better
to sort for deterministic output

refs tomnomnom/gron#102
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

4 participants