Hacker News new | ask | show | jobs
by Someone 1391 days ago
No, they aren’t. haasted replied to simonw’s

> "gojq does not keep the order of object keys" is a bit disappointing

with

> “I bet it's an artifact of Go having a randomized iteration order over maps. Getting a deterministic ordering requires extra work.”

But deterministic iteration order doesn’t imply that the order of keys is kept the same. There are map implementations that keep iteration follow insertion order, but the canonical map does not guarantee that. https://en.wikipedia.org/wiki/Associative_array#Hash_table_i...:

“The most frequently used general purpose implementation of an associative array is with a hash table: an array combined with a hash function that separates each key into a separate "bucket" of the array“

Such implementations iterate over maps in order of hash value (and hash collisions may or may not follow (reverse) insertion order)

1 comments

I don't think the distinction you're trying to make is helpful here if I've understood you correctly. A good faith interpretation of haastad's comment would be that they were thinking of "insertion order" when they said "a deterministic ordering". Even if we were being pedantic, their comment is still correct - for iteration order to be the same as input order then deterministic iteration ordering isn't sufficient (this seems to be the point you're making) but it is necessary.

Their first sentence:

> I bet it's an artifact of Go having a randomized iteration order over maps

is correct per Gojq's author [0]:

> gojq cannot implement keys_unsorted function because it uses map[string]interface{} to represent JSON object so it does not keep the order. Currently there is no plan to implement unordered map so I do not implement this function.

It would of course be possible to work around this limitation of Go's built-in map type but that's not the point. The author makes it clear that this limitation is the cause for Gojq's behaviour.

[0] https://github.com/itchyny/gojq/issues/50