# Efficient sorting of 2 Vectors with many duplicate values?

**URL:** https://discourse.julialang.org/t/efficient-sorting-of-2-vectors-with-many-duplicate-values/6156
**Category:** General Usage
**Tags:** sort, sortperm
**Created:** [September 29, 2017, 11:37am UTC](https://discourse.julialang.org/t/efficient-sorting-of-2-vectors-with-many-duplicate-values/6156 "2017-09-29T11:37:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [September 29, 2017, 11:37am UTC](https://discourse.julialang.org/t/efficient-sorting-of-2-vectors-with-many-duplicate-values/6156/1 "2017-09-29T11:37:29Z")

</div>

I have two vectors, `X`, `Y` that represent positions of 2D points. Both of these vectors have a lot of duplicates, but there is no duplicate point (i.e. many points have same `x` or `y` coords, but no points have both `x` and `y` the same).

I am trying to sort these vectors such that they are first sorted by increasing `X` and then by increasing `Y`. Is there a straight-forward way to do it?

My current approach is to first sort X. Then, for each section of X that has the same value, sort the corresponding section of Y. Get these sorting indices (with `sortperm`) and put their values in the master sorting vector. Continue until the end.

This seems kinda bad though 😛

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [September 29, 2017, 11:49am UTC](https://discourse.julialang.org/t/efficient-sorting-of-2-vectors-with-many-duplicate-values/6156/2 "2017-09-29T11:49:52Z")

</div>

You could represent your points as tuples:

```julia
julia> sort([(3,4), (3,2)])                                                                                                                           
2-element Array{Tuple{Int64,Int64},1}:                                                                                                                
 (3, 2)                                                                                                                                               
 (3, 4)

```

Or use `Point` from [GitHub - JuliaGeometry/GeometryTypes.jl: Geometry types for Julia](https://github.com/JuliaGeometry/GeometryTypes.jl), which I suspect would allow similar sorting. (Edit: I’m not sure `Point` actually exists, but something along those lines.)

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [September 29, 2017, 11:53am UTC](https://discourse.julialang.org/t/efficient-sorting-of-2-vectors-with-many-duplicate-values/6156/3 "2017-09-29T11:53:36Z")

</div>

Holy bananas it was that simple? Really? Damnit.

Yeap, using `sortperm(collect(zip(X,Y)))` does exactly what I need! and it is suprisingly fast as well!  
14ms for Vectors with 30,000 elements.
