# Values(dict) returns key-value pairs after sorting

**URL:** <https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422>\
**Category:** New to Julia\
**Tags:** dictionary\
**Created:** [August 11, 2019, 3:13pm UTC](https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422 "2019-08-11T15:13:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![felluksch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felluksch/32/10040_2.png) [@felluksch](https://discourse.julialang.org/u/felluksch)\
**Post date:** [August 11, 2019, 3:13pm UTC](https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422/1 "2019-08-11T15:13:59Z")

</div>

Hi,

```julia
arbeit = Dict("Vollzeit" => length(vollzeit), "Teilzeit" => length(teilzeit), "Minijob" => length(minijob))
arbeit = sort(collect(arbeit), by = x -> x[2])
println(arbeit)
x = collect(keys(arbeit))
println(x)
y = collect(values(arbeit))
println(y)

```

If line 2 is commented out, `println(y)` prints the (unsorted) length values as expected and `println(x)` the corresponding keys. If not, `println(y)` returns the whole dict and `println(x)` returns an array [1:8]. Why? How can I get the keys and values of a sorted dict?  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [August 11, 2019, 3:32pm UTC](https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422/2 "2019-08-11T15:32:56Z")

</div>

Heya,

by `collect`ing a `Dict`, you turn it into a vector of `Pair`s. The default dict is inherently unordered and so sorting it does not make sense but sorting the collection makes sense.  
In your second line, you assign a (sorted) vector of `Pair`s to arbeit. We don’t usually consider a vector as having keys but for generality, we can think of the index of an element as its key (same as for the dict where we have `dict[key] = value` we have `vector[index] = value`). That’s why you get a range for the keys: it’s the indices, whereas the values are the actual pairs of the dict.

So anyway, to your question. You can either use a sorted `Dict` (from `DataStructures.jl`) or

1. turn the dict into a vector (of pairs)
2. sort that vector according to your wishes
3. extract the `first` and `second` component of your `Pair`

```julia
arbeit = Dict("Vollzeit" => length(vollzeit), "Teilzeit" => length(teilzeit), "Minijob" => length(minijob))
arbeit = sort(collect(arbeit), by = x -> x[2])
x = map(x -> x[1], arbeit)
y = map(x -> x[2], arbeit)

```

Note that it’s usually easier to help if your example is self-contained, e.g. by providing dummy values for `vollzeit` etc. or the dictionary itself.

---

<div class="post-metadata">

**Author:** ![felluksch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felluksch/32/10040_2.png) [@felluksch](https://discourse.julialang.org/u/felluksch)\
**Post date:** [August 11, 2019, 3:54pm UTC](https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422/3 "2019-08-11T15:54:44Z")

</div>

Thanks for the comprehensive explanation, really useful. I will make my code examples self-contained in the future, thanks for the hint.  
Just a quick follow-up question: Are `first` and `second` meant to be placeholders for `x -> x[i]` or are you using a special library or something for these shortcuts? `first` threw an error in my case.

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [August 11, 2019, 4:03pm UTC](https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422/4 "2019-08-11T16:03:53Z")

</div>

Oh sorry, I didn’t test my code and misread something in the documentation for `Pair` - if you have a pair, it has _fields_ `first` and `second` - I though those were functions.  
So you can either do `x -> x.first` and `x -> x.second` or just use indices. I edited my answer to reflect that - sorry for the confusion.

(although I think `first` should work anyway, being a function that generally works for iterables. `second` should throw an error though)

---

<div class="post-metadata">

**Author:** ![felluksch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/felluksch/32/10040_2.png) [@felluksch](https://discourse.julialang.org/u/felluksch)\
**Post date:** [August 11, 2019, 4:59pm UTC](https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422/5 "2019-08-11T16:59:30Z")

</div>

Ah, yeah, `second` threw the error, sorry. Thanks anyway!

---

<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:** [August 11, 2019, 5:36pm UTC](https://discourse.julialang.org/t/values-dict-returns-key-value-pairs-after-sorting/27422/6 "2019-08-11T17:36:14Z")

</div>

I believe you can also use the last function.
