# Way to recreate OrderedDict under new sorting function?

**URL:** <https://discourse.julialang.org/t/way-to-recreate-ordereddict-under-new-sorting-function/24900>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 3, 2019, 10:49pm UTC](https://discourse.julialang.org/t/way-to-recreate-ordereddict-under-new-sorting-function/24900 "2019-06-03T22:49:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [June 3, 2019, 10:49pm UTC](https://discourse.julialang.org/t/way-to-recreate-ordereddict-under-new-sorting-function/24900/1 "2019-06-03T22:49:00Z")

</div>

Lets say you have an ordered dict:

```julia
using DataStructures
cur_dict = OrderedDict()

```

And you then do two rounds of key inserts:

```julia
foreach(cur_value -> cur_dict[cur_value] = cur_value+rand()/2, 1:2:9)
foreach(cur_value -> cur_dict[cur_value] = cur_value+rand()/4, 2:4:10)

```

This gives:

```julia
> cur_dict
OrderedDict{Any,Any} with 8 entries:
  1 => 1.14616
  3 => 3.2501
  5 => 5.26185
  7 => 7.40121
  9 => 9.20038
  2 => 2.02592
  6 => 6.11011
  10 => 10.2155

```

Is there a way to reorder the `OrderedDict`, so that the keys are sorted under some new sort function?

// i.e. by `sort(collect(keys(cur_dict))) == [1, 2, 3, 5, 6, 7, 9, 10]`

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 4, 2019, 2:36am UTC](https://discourse.julialang.org/t/way-to-recreate-ordereddict-under-new-sorting-function/24900/2 "2019-06-04T02:36:08Z")

</div>

A simple way is to just create a new OrderedDict.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 4, 2019, 3:37am UTC](https://discourse.julialang.org/t/way-to-recreate-ordereddict-under-new-sorting-function/24900/3 "2019-06-04T03:37:43Z")

</div>

> `OrderedDicts` are simply dictionaries whose entries have a particular order. For `OrderedDicts` (and `OrderedSets` ), order refers to _insertion order_ , which allows deterministic iteration over the dictionary or set

So there is no sort function beyond insertion order. You could presort your information and then insert it in that sorted order.

Maybe a `SortedDict` is better for you. see [https://juliacollections.github.io/DataStructures.jl/latest/sorted\_containers.html](https://juliacollections.github.io/DataStructures.jl/latest/sorted_containers.html)
