# Ordered container clarification

**URL:** <https://discourse.julialang.org/t/ordered-container-clarification/49125>\
**Category:** New to Julia\
**Created:** [October 27, 2020, 2:50pm UTC](https://discourse.julialang.org/t/ordered-container-clarification/49125 "2020-10-27T14:50:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon60034542](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@anon60034542](https://discourse.julialang.org/u/anon60034542)\
**Post date:** [October 27, 2020, 2:50pm UTC](https://discourse.julialang.org/t/ordered-container-clarification/49125/1 "2020-10-27T14:50:05Z")

</div>

From the [push()!](https://docs.julialang.org/en/v1/base/collections/#Base.push!) documentation:

> Insert one or more `items` in `collection` . If `collection` is an ordered container, the items are inserted at the end (in the given order).

What does ordered mean is this case? That the collection preservers the insertion order? The example uses arrays, but arrays don’t sort as you insert the elements, and I can add elements to the end of an unsorted array:

```julia
unsorted_numbers = Int32[5,47,22,15,3,10]
push!(unsorted_numbers, 32)
println(unsorted_numbers)

Output:
[5, 47, 22, 15, 3, 10, 32]

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 27, 2020, 2:58pm UTC](https://discourse.julialang.org/t/ordered-container-clarification/49125/2 "2020-10-27T14:58:17Z")

</div>

> [@anon60034542](#):
>
> That the collection preservers the insertion order?

That’s a way of phrasing it. But it really means what it says above. “Order” in this context does not imply sorting.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 27, 2020, 4:21pm UTC](https://discourse.julialang.org/t/ordered-container-clarification/49125/3 "2020-10-27T16:21:23Z")

</div>

I’m not sure, but if you actually wanted _sorted_ containers, they are here (none that I know of are in ~~the standard library~~ Base, meaning in Julia; that package can however be seen as the standard):  
[https://juliacollections.github.io/DataStructures.jl/v0.9/sorted\_containers.html](https://juliacollections.github.io/DataStructures.jl/v0.9/sorted_containers.html)

Arrays (and lists, neither in Julia Base; available in the package above) are naturally ordered. Dicts (and sets), are often unordered, there’s are many different kinds, some unordered, some ordered, some sorted, while only unordered `Dict` and `Set` are in ~~the standard library, meaning~~ Julia Base.

For ordered dict, the most used is OrderedDict (while it’s not the fastest option).

E.g. unlike:

```julia
julia> d = Dict("A" => 1, "B" => 2)
Dict{String,Int64} with 2 entries:
  "B" => 2
  "A" => 1

```

You see it right away, this time, I guess you have ~~a 50% change, for two values~~ (it seems lower, just beware) of actually unordered, at least not for:

```julia
d = Dict("B" =>2, "C" => 3)
Dict{String,Int64} with 2 entries:
  "B" => 2
  "C" => 3

```

And deceptively `push!` seems to add to the end:

```julia
julia> push!(d, "C" => 3)
Dict{String,Int64} with 3 entries:
  "B" => 2
  "C" => 3

```

The pattern seems to hold adding for the next 3 push!, until:

```julia
julia> push!(d, "G" => 7)
Dict{String,Int64} with 7 entries:
  "B" => 2
  "C" => 3
  "D" => 4
  "G" => 7
  "E" => 5
  "F" => 6

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 28, 2020, 8:49am UTC](https://discourse.julialang.org/t/ordered-container-clarification/49125/4 "2020-10-28T08:49:49Z")

</div>

> [@Palli](#):
>
> deceptively `push!` seems to add to the end

No, it isn’t deceptive; `Dict` is not ordered and entries just happen to be printed some way. The concept of “end” does not apply here.

> [@Palli](#):
>
> The pattern seems to hold

There is no pattern.
