# Sorting in-place several Vector's simultaneously

**URL:** <https://discourse.julialang.org/t/sorting-in-place-several-vectors-simultaneously/125665>\
**Category:** Performance\
**Tags:** performance, sort\
**Created:** [February 7, 2025, 5:27pm UTC](https://discourse.julialang.org/t/sorting-in-place-several-vectors-simultaneously/125665 "2025-02-07T17:27:33Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Leo\_I](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leo_i/32/27262_2.png) [@Leo\_I](https://discourse.julialang.org/u/Leo_I)\
**Post date:** [February 10, 2025, 6:47pm UTC](https://discourse.julialang.org/t/sorting-in-place-several-vectors-simultaneously/125665/21 "2025-02-10T18:47:53Z")

</div>

@eldee I just wish to thank you for your help. : ) In the last 3 days, I’ve learnt something new (@generated) and I think it’s super cool that Julia allows us to hack into its functionality so easily (in my case, using all of the sorting capabilities on my custom struct so long as I define `size`, `length`, `getindex`, `setindex`) without losing any of the performance!!!

For anyone else interested in this, I am posting the finalized code and its benchmarks:

```julia
struct Zip{N, T<:Tuple} <: AbstractVector{Tuple}
   vec::T
   Zip{N,T}(vecs::T) where {N,T} = length(vecs)==N ? new(vecs) : error("lengths don't match!") 
end
Zip(vecs) = Zip{length(vecs), typeof(vecs)}(vecs)
Base.size(z::Zip) = (length(z),) # for AbstractVector compliance
@generated function Base.length(z::Zip{N,T}) where {N,T}
   Expr(:call, :min, (:(length(z.vec[$j])) for j=1:N)...) end
@generated function Base.getindex(z::Zip{N,T}, i::Int) where {N,T}
   Expr(:tuple, (:(@inbounds(z.vec[$j][i])) for j=1:N)...) end
@generated function Base.setindex!(z::Zip{N,T}, vals::Tuple, i::Int) where {N,T}
   Expr(:block, (:(@inbounds(z.vec[$j][i] = vals[$j])) for j=1:N)...) end;

function tmp!(x,y)  
   n = min(length.((x,y))...)
   p = sortperm(x)[1:n]
   view(x,1:n) .= x[p]
   view(y,1:n) .= y[p] 
end

julia> begin n=10^6;   
   @btime sort!(x, alg=QuickSort) setup=(x=rand(1:$n,$n));                    
   @btime tmp!(x,y) setup=(x=rand(1:$n,$n); y=rand(1.0:$n,$n+10));  
   @btime sort!(Zip((x,y)), by=first, alg=QuickSort) setup=(x=rand(1:$n,$n); y=rand(1.0:$n,$n+10)); 
   @btime sort!(StructArray((x,view(y,1:$n))), by=first, alg=QuickSort) setup=(x=rand(1:$n,$n); y=rand(1.0:$n,$n+10)); end; 
  71.717 ms (0 allocations: 0 bytes)
  159.054 ms (15 allocations: 38.15 MiB)
  75.413 ms (0 allocations: 0 bytes)
  81.581 ms (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 3, 2025, 1:26pm UTC](https://discourse.julialang.org/t/sorting-in-place-several-vectors-simultaneously/125665/22 "2025-03-03T13:26:53Z")

</div>

> [@Leo\_I](#):
>
> ```julia
> @generated function Base.length(z::Zip{N,T}) where {N,T}
> Expr(:call, :min, (:(length(z.vec[$j])) for j=1:N)...) end
> 
> ```

I don’t think you need `@generated` for this sort of thing. The [`ntuple` function](https://docs.julialang.org/en/v1/base/base/#Base.ntuple) is your friend here. For example:

```julia
Base.length(z::Zip{N}) where {N} = minimum(ntuple(j -> length(z.vec[j]), Val{N}()))

```

should do the trick, I think?

> [@Leo\_I](#):
>
> `AbstractVector{Tuple}`

Note that this is this container type has abstractly typed elements (it indicates that every element could be a _different_ type of `Tuple`), and is probably not what you want:

```julia
julia> AbstractVector{Tuple{Int,Int}} <: AbstractVector{Tuple}
false

```

(Implementing this kind of thing properly is tricky, but should be taken care of by StructArrays.jl here.)

[Previous page](https://discourse.julialang.org/t/sorting-in-place-several-vectors-simultaneously/125665.md?page=1)
