# SORT a Dictionary by Numerical Keys

**URL:** <https://discourse.julialang.org/t/sort-a-dictionary-by-numerical-keys/61547>\
**Category:** General Usage\
**Tags:** dictionary\
**Created:** [May 20, 2021, 10:52pm UTC](https://discourse.julialang.org/t/sort-a-dictionary-by-numerical-keys/61547 "2021-05-20T22:52:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bonnkleiford](https://avatars.discourse-cdn.com/v4/letter/b/46a35a/32.png) [@bonnkleiford](https://discourse.julialang.org/u/bonnkleiford)\
**Post date:** [May 20, 2021, 10:52pm UTC](https://discourse.julialang.org/t/sort-a-dictionary-by-numerical-keys/61547/1 "2021-05-20T22:52:22Z")

</div>

I have a Dictionary:

```julia
A = Dict("1" => 0.1,
  "11" => 0.1,
  "13" => 0.1,
  "15" => 0.1,
  "17" => 0.1,
  "19" => 0.1,
  "3" => 0.1,
  "5" => 0.1,
  "7" => 0.1,
  "9" => 0.1)

```

I want to sort it according to its numerical keys. I tried this:

`sort(A)`

But it comes out as:

```julia
OrderedCollections.OrderedDict{String, Float64} with 10 entries:
  "1" => 0.1
  "11" => 0.1
  "13" => 0.1
  "15" => 0.1
  "17" => 0.1
  "19" => 0.1
  "3" => 0.1
  "5" => 0.1
  "7" => 0.1
  "9" => 0.1

```

But I want:

```julia
OrderedCollections.OrderedDict{String, Float64} with 10 entries:
  "1" => 0.1
  "3" => 0.1
  "5" => 0.1
  "7" => 0.1
  "9" => 0.1
  "11" => 0.1
  "13" => 0.1
  "15" => 0.1
  "17" => 0.1
  "19" => 0.1

```

How do I do this?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [May 21, 2021, 12:02am UTC](https://discourse.julialang.org/t/sort-a-dictionary-by-numerical-keys/61547/2 "2021-05-21T00:02:46Z")

</div>

The problem is those aren’t numerical keys, they are strings. And the string “11” comes before the string “3” (etc.)

It seems in this case it makes more sense to have actual numbers as the keys, which would make this work exactly as you expected. If you can’t do that for whatever reason, you can also pass in a function to sort by:

```julia
sort(A, by = x -> parse(Int, x))
```

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [May 21, 2021, 12:04am UTC](https://discourse.julialang.org/t/sort-a-dictionary-by-numerical-keys/61547/3 "2021-05-21T00:04:55Z")

</div>

or you might want to use `"03"` instead of `"3"`.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 21, 2021, 5:44am UTC](https://discourse.julialang.org/t/sort-a-dictionary-by-numerical-keys/61547/4 "2021-05-21T05:44:35Z")

</div>

[https://github.com/JuliaStrings/NaturalSort.jl](https://github.com/JuliaStrings/NaturalSort.jl)
