OrderedDict from OrderedCollections.jl

I tried merging two OrderedDicts with merge (also with merge!). The resulting dictionary was of type OrderedDict but was not Ordered!!!

Can you give an example of the behavior you get and the behavior you expect?

To me it looks OK:

julia> using OrderedCollections

julia> d1 = OrderedDict("a" => 1, "b" => 2)
OrderedDict{String, Int64} with 2 entries:
  "a" => 1
  "b" => 2

julia> d2 = OrderedDict("c" => 3, "a" => 4)
OrderedDict{String, Int64} with 2 entries:
  "c" => 3
  "a" => 4

julia> merge!(d1, d2)
OrderedDict{String, Int64} with 3 entries:
  "a" => 4
  "b" => 2
  "c" => 3

Here all the order in d1 is preserved and the new elements of d2 are added at the end (which makes sense to me since I am inserting them later, via the merge! operation).

1 Like

My two dictionaries were Float64 to Float64.

It’s probably worth noting that OrderedDict is ordered by insertion time, not value. You might be looking for a SortedDict.

4 Likes

Thank you, that explains it.