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

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)?

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

1 Like

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> 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

To build on your example:

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> 

Set is a mathematical concept defined by “Two sets are equal if and only if they have precisely the same elements” (Wikipedia). 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.

5 Likes

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

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.

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.

3 Likes