# Dictionary values ascending or descending

**URL:** <https://discourse.julialang.org/t/dictionary-values-ascending-or-descending/74492>\
**Category:** New to Julia\
**Tags:** dictionary, sorting\
**Created:** [January 12, 2022, 2:55pm UTC](https://discourse.julialang.org/t/dictionary-values-ascending-or-descending/74492 "2022-01-12T14:55:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderChen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexanderchen/32/22007_2.png) [@AlexanderChen](https://discourse.julialang.org/u/AlexanderChen)\
**Post date:** [January 12, 2022, 2:55pm UTC](https://discourse.julialang.org/t/dictionary-values-ascending-or-descending/74492/1 "2022-01-12T14:55:41Z")

</div>

Hi,

I have a dictionary of characters and their frequency in a text(or word):

```julia
  'H' => 1
  'l' => 2
  'e' => 1
  'o' => 1

```

and I want to order it by value so:

```julia
  'H' => 1
  'e' => 1
  'o' => 1
  'l' => 2

```

the order of ‘H’, ‘e’ or ‘o’ is irrelevant. It simply needs to be ordered either by ascending or descending value order. Is there a built in function for that?

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 12, 2022, 3:01pm UTC](https://discourse.julialang.org/t/dictionary-values-ascending-or-descending/74492/2 "2022-01-12T15:01:35Z")

</div>

```julia
julia> d = Dict('H' => 1, 'l' => 2, 'e' => 1,'o' => 1)
julia> sort(collect(keys(d)); lt=(a,b)->d[a]<d[b])
4-element Vector{Char}:
 'H': ASCII/Unicode U+0048 (category Lu: Letter, uppercase)
 'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
 'o': ASCII/Unicode U+006F (category Ll: Letter, lowercase)
 'l': ASCII/Unicode U+006C (category Ll: Letter, lowercase)

```

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [January 12, 2022, 3:08pm UTC](https://discourse.julialang.org/t/dictionary-values-ascending-or-descending/74492/3 "2022-01-12T15:08:25Z")

</div>

my previous reply was not correct, I have edited it but someone pressed the like, hence this one

My use of Revise was interfering

> [@\`using\` a package that uses \`OrderedCollections\` can silently add function methods?](https://discourse.julialang.org/t/using-a-package-that-uses-orderedcollections-can-silently-add-function-methods/60741/5):
>
> Great, thanks for the clarifications!

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 12, 2022, 3:10pm UTC](https://discourse.julialang.org/t/dictionary-values-ascending-or-descending/74492/4 "2022-01-12T15:10:58Z")

</div>

You can use the `by` keyword (and also the `rev` keyword if you need descending order):

```julia
julia> sort(collect(d), by = x -> x.second)
4-element Vector{Pair{Char, Int64}}:
 'H' => 1
 'e' => 1
 'o' => 1
 'l' => 2

julia> sort(collect(d), by = x -> x.second, rev = true)
4-element Vector{Pair{Char, Int64}}:
 'l' => 2
 'H' => 1
 'e' => 1
 'o' => 1

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 12, 2022, 3:12pm UTC](https://discourse.julialang.org/t/dictionary-values-ascending-or-descending/74492/5 "2022-01-12T15:12:27Z")

</div>

> [@juliohm](#):
>
> `sort(collect(d), by = x -> x.second)`

Or `sort!(collect(d), by=last)`, which avoids allocating a second array and utilizes `last` for conciseness.
