# Can you sort a Set?

**URL:** https://discourse.julialang.org/t/can-you-sort-a-set/47948
**Category:** New to Julia
**Tags:** sort, sets
**Created:** [October 7, 2020, 4:46pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948 "2020-10-07T16:46:09Z")
**Posts on this page:** 10
**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 7, 2020, 4:46pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/1 "2020-10-07T16:46:09Z")

</div>

In Python, I can do:

```python
a = set([2,1,5,3,2])
b = sorted(a)
print(b) # Returns a sorted List

```

In Julia, Is it possible to sort a `Set`?

**Edit** : There is no particular reason why I require a `Set`. I was reading the documentation and attempted to sort a simple `Set`, and was puzzled by the error returned.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [October 7, 2020, 4:52pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/2 "2020-10-07T16:52:35Z")

</div>

No. You can however convert it to a vector and sort that:

```julia
julia> a = Set([2,1,5,3,2]); sort!(collect(a))
4-element Array{Int64,1}:
 1
 2
 3
 5

```

---

<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 7, 2020, 5:01pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/3 "2020-10-07T17:01:26Z")

</div>

Any reason why this wasn’t implemented?

---

<div class="post-metadata">

### Author: ![Jordan\_Cluts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jordan_cluts/32/13753_2.png) [@Jordan\_Cluts](https://discourse.julialang.org/u/Jordan_Cluts)
#### Post date: [October 7, 2020, 5:14pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/4 "2020-10-07T17:14:54Z")

</div>

The Set type is built on top of the Dict type which has no ordering so there is no way for the set to “order” the items in any particular way.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 7, 2020, 5:22pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/5 "2020-10-07T17:22:55Z")

</div>

`sorted(a)` isn’t actually sorting the set itself in python either–it’s creating a new list from the elements of the set and sorting it. It should be quite similar to doing `sort(collect(a))` in Julia. You could even define a helper function in your own code:

```julia
sorted(x) = sort(collect(x))

```

Alternatively, if you want a set that maintains its own sorted order, you could use an `OrderedSet` from [OrderedDicts and OrderedSets · OrderedCollections.jl](https://juliacollections.github.io/OrderedCollections.jl/latest/ordered_containers.html)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [October 7, 2020, 6:23pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/6 "2020-10-07T18:23:27Z")

</div>

I think the larger question is why you would use a `Set` if the objects have an order relation.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [October 7, 2020, 7:04pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/7 "2020-10-07T19:04:41Z")

</div>

This package might also have something useful for you

> **[GitHub - JuliaCollections/DataStructures.jl: Julia implementation of Data...](https://github.com/JuliaCollections/DataStructures.jl)**
>
> Julia implementation of Data structures. Contribute to JuliaCollections/DataStructures.jl development by creating an account on GitHub.

---

<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: [October 7, 2020, 8:08pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/8 "2020-10-07T20:08:52Z")

</div>

Because inserting into a sorted vector is not efficient?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [October 7, 2020, 8:37pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/9 "2020-10-07T20:37:38Z")

</div>

This assumes there will be a collection of values that needs to be sorted all the time and you also need to insert values between tests. The even larger question is which is the use case?

- If only membership is needed, the `Set` would be the right structure.
- If membership and ordering are important, but there are not many inserts, or the collection size is small, then a sorted `Vector` is probably the best.
- If membership, ordering, and repeated/interleaved insertion is needed, then `SortedSet` is probably the best.
- There is also the question if arbitrary removal is needed, if it is then `Set`/`SortedSet` are prefered over a sorted `Vector`.

Without knowing the exact use case it is impossible to prescribe a good solution, but `Set` then `sort(collect(...))` does not seem a good choice in many cases, it shines only if: there can be no repetition of elements; there will be a lot of insertions/removals/‘membership tests’; and after all of these, the remaining values need to be returned as a sorted sequence.

---

<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: [October 7, 2020, 9:29pm UTC](https://discourse.julialang.org/t/can-you-sort-a-set/47948/10 "2020-10-07T21:29:54Z")

</div>

Depending on the use case a `PriorityQueue` may be a good option.
