As by the docs, for OrderedDict
order refers to insertion order.
I don't know how this can "lose its order down the line", but maybe you are just mutating stuff?
Probably what you want is closer to a SortedDict; however, this sorts by keys, not values. A dictionary sorted by values is a bit of an unusual application.
If you want a mutable data structure with fast lookup by key and iteration sorted by value, you could emulate this by a two-level approach: a normal dict for storing a mapping between original keys and tokens, and a second SortedMultiDict{ValueType, Nothing} to emulate a sorted multiset into which the tokens index. Then you define your own mechanism for indirect lookup through tokens somehow like this:
function insert!(d::ValueSortedDict, k, v)
_, token = insert!(d.values, v, nothing)
d.keys[k] = token
end
getindex(d::ValueSortedDict, k) = deref_key((d.values, d.keys[k]))
And accordingly for other get/set style functions. (I didn't test this, it's just read off the docs.)
OTOH, if you never intend to mutate things, you can just do a very similar thing where you store a Dict{KeyType, Int} and a Vector{ValueType} together and sort! the vector once at the beginning. (Dictionaries.jl, described in @mcabbot's answer, is basically implementing this.)