Setting the display order of entries in (unordered) Dict

Is there a way to designate what order the pairs of a Julai dictionary are displayed in (without using OrderedDict)?

Elaboration: StructuralIdentifiability.jl takes a model, and assesses the identifiability of each parameter and initial condition. The output is a dict, mapping each component to its identifiability. I am making an extension which applies its functions to reaction network models (declared by Catalyst). It works, but the output jumbles parameters and initial conditions a little bit all over the place. For the user, it would be easier to go through the output if they were in a specific order. So ideally I would like to remake the output Dict before it is sent to the user, fixing the order which things are displayed. However, given that StrucutrualIdentifaibility.jl’s functions outputs (unordered) Dictionaries, I want my output of these functions to output the same type (this feels sensible), and hence not use OrderedDictionaries. Also, I do not care about the order for iterating through the dict or similarly, this is all about how it is displayed.

I tried just creating a new dict, and setting the entires one by one in the designated order. Unfortunately, this did not work.

I realise this might not be possible, but figured I’d ask at least.

A Dict’s display order is (almost certainly, but I’m speculating) based on its storage order. Because a Dict organizes keys by hash (for efficient lookup), this is not something you have good control over. Assembling the Dict in a different order will not affect the storage order in a predictable way and it would be subject to change over different versions anyway.

If this is just for display, consider collecting the Dict to another container where you have better control and instead display that. For example

julia> sort(Dict(3 => 2, 1 => 4)) # OrderedDict
OrderedCollections.OrderedDict{Int64, Int64} with 2 entries:
  1 => 4
  3 => 2

julia> sort!(collect(Dict(3 => 2, 1 => 4))) # Vector{Pair}
2-element Vector{Pair{Int64, Int64}}:
 1 => 4
 3 => 2
1 Like

Sounds like what I feared is the case, and there isn’t really a solution without going to OrderedDicts :frowning: . Thanks for the input!