# How to sort NTuple

**URL:** <https://discourse.julialang.org/t/how-to-sort-ntuple/11535>\
**Category:** General Usage\
**Tags:** sort, ntuple\
**Created:** [June 8, 2018, 11:50am UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535 "2018-06-08T11:50:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 8, 2018, 11:50am UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535/1 "2018-06-08T11:50:25Z")

</div>

I have several integers input from a function like this

```julia
function foo(a::Int...)
end

```

How do I sort a? I have to collect it first and sort it as a vector at the moment. Will it possible to sort it directly?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [June 8, 2018, 12:01pm UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535/2 "2018-06-08T12:01:10Z")

</div>

You can use [https://github.com/JeffreySarnoff/SortingNetworks.jl:](https://github.com/JeffreySarnoff/SortingNetworks.jl:)

```julia
julia> using SortingNetworks

julia> function foo(a::Int...)
           return swapsort(a)
       end
foo (generic function with 1 method)

julia> foo(5, 2, 4, 3, 1)
(1, 2, 3, 4, 5)

```

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [June 8, 2018, 1:34pm UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535/4 "2018-06-08T13:34:41Z")

</div>

Also, see TupleTools.jl

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [June 8, 2018, 3:17pm UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535/5 "2018-06-08T15:17:43Z")

</div>

Also, if your NTuple happens to live on the heap, consider `Base.sort!` with a pointer.

Re `SortingNetworks.jl`: This is really cool! But somehow 0.6 refuses to emit the good (`CMOV` instead of branch) code for small integers ☹ (this gives an ~8x slowdown for `Int32`, `Int16`, `Int8` compared to `Int64`, `UInt64`; for some reasons, floats are slow anyway)

Interpreting a pointer as a small array:

```julia
struct ptrArray{N,T}<:AbstractVector{T}
    ptr::Ptr{T}
end
ptrArray(ptr::Ptr{NTuple{N,T}}) where {N,T} = ptrArray{N,T}(convert(Ptr{T}, ptr))
Base.length(x::ptrArray{N,T}) where {N,T} =N 
Base.size(x::ptrArray{N,T}) where {N,T} =(N,) 
Base.getindex(x::ptrArray,i) = unsafe_load(x.ptr,i)
Base.setindex!(x::ptrArray,v,i) = unsafe_store!(x.ptr,v,i)
Base.IndexStyle(::ptrArray) = Base.IndexLinear()

```

Postscript: 0.7 fixes this on small integers (CMOV instead of branch), but still fails to generate branch-free code for floats (missing `@fastmath` leading to very complicated `NaN`-aware `minmax`?). I wonder whether someone cooked up sorting networks for vector instructions.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 9, 2018, 12:29am UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535/6 "2018-06-09T00:29:59Z")

</div>

Thanks for kind thoughts and _hoorah_ for v0.7!

---

<div class="post-metadata">

**Author:** ![Lilith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lilith/32/27492_2.png) [@Lilith](https://discourse.julialang.org/u/Lilith)\
**Post date:** [January 5, 2025, 12:57pm UTC](https://discourse.julialang.org/t/how-to-sort-ntuple/11535/7 "2025-01-05T12:57:53Z")

</div>

`sort(a)` as of Julia 1.12 ([Add sort for NTuples by LilithHafner · Pull Request #54494 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/54494))
