# What is the order of element in collect(s::Set)?

**URL:** <https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094>\
**Category:** New to Julia\
**Created:** [July 22, 2021, 9:14am UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094 "2021-07-22T09:14:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Post date:** [July 22, 2021, 9:14am UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/1 "2021-07-22T09:14:03Z")

</div>

Does anyone know what governs this? Is it safe to assume that the order is fixed as is multiple call to `collect(s)` gives the same Array (based on my limited experimentation)?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 22, 2021, 9:26am UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/2 "2021-07-22T09:26:29Z")

</div>

> [@Thomas](#):
>
> gives the same Array

probably, but you shouldn’t rely on this because `Set` is designed to have arbitrary order when you collect/iterate through them.

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [July 22, 2021, 9:31am UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/3 "2021-07-22T09:31:41Z")

</div>

For a given Julia version, if the set is constructed in the same way, the order of iteration is fixed. The second condition matters, as e.g. on the version I use, I get:

```julia
julia> Set([11, 2])
Set{Int64} with 2 elements:
  11
  2

julia> Set([2, 11])
Set{Int64} with 2 elements:
  2
  11

julia> Set([2, 11]) == Set([11, 2])
true

```

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Post date:** [July 22, 2021, 9:34am UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/4 "2021-07-22T09:34:32Z")

</div>

To build on your example:

```julia
julia> s = Set([11,2])
Set{Int64} with 2 elements:
  11
  2

julia> ss = Set([2,11])
Set{Int64} with 2 elements:
  2
  11

julia> s == ss
true

julia> collect(s)
2-element Vector{Int64}:
 11
  2

julia> collect(s)
2-element Vector{Int64}:
 11
  2

julia> collect(ss)
2-element Vector{Int64}:
  2
 11

julia> collect(ss)
2-element Vector{Int64}:
  2
 11

julia> collect(s) == collect(ss)
false

julia> 

```

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [July 22, 2021, 10:02am UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/5 "2021-07-22T10:02:13Z")

</div>

Set is a mathematical concept defined by “Two sets are equal [if and only if](https://en.wikipedia.org/wiki/If_and_only_if) they have precisely the same elements” ([Wikipedia](https://en.wikipedia.org/wiki/Set_(mathematics))). Sets are useful if you want to remove duplicates and for cases where you don’t care about the ordering.

Therefore, it’s not a good idea to assume the order is fixed when you do `collect(s)`.

Depending on what you need exactly, maybe `sort(collect(s))` would be a solution.

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [July 22, 2021, 1:22pm UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/6 "2021-07-22T13:22:30Z")

</div>

Or if you need to preserve the ordering of the first time an element is encountered using the function `unique` will work, instead of converting to a set and back.  
[https://docs.julialang.org/en/v1/base/collections/#Base.unique](https://docs.julialang.org/en/v1/base/collections/#Base.unique)

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://discourse.julialang.org/u/Thomas)\
**Post date:** [July 22, 2021, 1:41pm UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/7 "2021-07-22T13:41:16Z")

</div>

Thanks guys. I have no clue how `Set` works internally (probably build upon `Dict` or `Array`), and based on this `collect` would probably give a “natural” ordering in the resulting `Array` container. I am purely guessing here but the order could depends on memory layout.

@WschW thanks for this tip (I was not aware of the order-preserving property of `unique`) but it is not what I am after.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 22, 2021, 3:36pm UTC](https://discourse.julialang.org/t/what-is-the-order-of-element-in-collect-s-set/65094/8 "2021-07-22T15:36:47Z")

</div>

`Set` does not guarantee any kind of ordering, it’s by definition an unordered data structure.

DataStructures.jl has an `OrderedSet` type which guarantees the insertion order when iterated over.
