# How to get two dictionaries with the same ordering of the keys

**URL:** <https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 19, 2023, 6:57pm UTC](https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572 "2023-06-19T18:57:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fatemeh\_haddadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatemeh_haddadi/32/50868_2.png) [@fatemeh\_haddadi](https://discourse.julialang.org/u/fatemeh_haddadi)\
**Post date:** [June 19, 2023, 6:57pm UTC](https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572/1 "2023-06-19T18:57:54Z")

</div>

Let’s say I have two dictionaries with the same keys but different values and different ordering:

```julia
D1 = Dict("B" => 10, "A" => 15, "C" => 17)
D2 = Dict("C" => 14, "B" => 11, "A" =>0)

```

I want to order dictionary D2 with the same key ordering as in dictionary D1. So I want to get:

```julia
D2= "B" => 11
    "A" => 0
    "C" => 14

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [June 19, 2023, 7:02pm UTC](https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572/2 "2023-06-19T19:02:28Z")

</div>

The [`Dict`](https://docs.julialang.org/en/v1/base/collections/#Base.Dict) type in `Base` is unordered. If you care about order of elements you should use some other data structures, for example like `OrderedDict` from [`OrderedCollections.jl`](https://github.com/JuliaCollections/OrderedCollections.jl).

---

<div class="post-metadata">

**Author:** ![fatemeh\_haddadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatemeh_haddadi/32/50868_2.png) [@fatemeh\_haddadi](https://discourse.julialang.org/u/fatemeh_haddadi)\
**Post date:** [June 19, 2023, 7:08pm UTC](https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572/3 "2023-06-19T19:08:59Z")

</div>

Thank you for your relpy!  
I had ordered D1 based on its values using:  
`D1= sort(unordered_D1; byvalue=true)`  
Are you saying even that ordering is not kept?

---

<div class="post-metadata">

**Author:** ![fatemeh\_haddadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatemeh_haddadi/32/50868_2.png) [@fatemeh\_haddadi](https://discourse.julialang.org/u/fatemeh_haddadi)\
**Post date:** [June 19, 2023, 7:10pm UTC](https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572/4 "2023-06-19T19:10:35Z")

</div>

So the idea was to order D1 first based on its values and then order D2 based on D1 keys.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [June 19, 2023, 7:14pm UTC](https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572/5 "2023-06-19T19:14:17Z")

</div>

> [@fatemeh\_haddadi](#):
>
> I had ordered D1 based on its values using:  
> `D1= sort(unordered_D1; byvalue=true)`  
> Are you saying even that ordering is not kept?

Yes, that’s not reliable, because `Dict` isn’t sorted, trying to later access the items you can get them in different order. Again, if order is really important you should use something else.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [June 19, 2023, 7:40pm UTC](https://discourse.julialang.org/t/how-to-get-two-dictionaries-with-the-same-ordering-of-the-keys/100572/6 "2023-06-19T19:40:36Z")

</div>

> [@giordano](#):
>
> you should use some other data structures

Another option is Dictionaries.jl
