if you need performance, keep in mind that
using SplitApplyCombine, BenchmarkTools, Dictionaries
A=[Tuple(rand(1:100, 4)) for _ in 1:10^5]
function groupbynth(A,n, sieve)
d=Dictionary{Int64, Vector{Tuple}}()
foreach(tA-> tA[1] β sieve ? push!(get!(()->Vector{Tuple}(), d, tA[n]),tA) : nothing, A)
d
end
julia> @btime groupbynth(A,3, [11, 8, 3])
893.400 ΞΌs (3354 allocations: 202.81 KiB)
100-element Dictionary{Int64, Vector{Tuple}}
4 β Tuple[(11, 35, 4, 28), (3, 93, 4, 98), (11, 82, 4, 97), (3, 75, 4, 76), (β¦
38 β Tuple[(8, 63, 38, 95), (11, 46, 38, 23), (3, 3, 38, 1), (3, 70, 38, 80), β¦
19 β Tuple[(8, 67, 19, 42), (11, 13, 19, 78), (11, 88, 19, 10), (3, 10, 19, 89β¦
42 β Tuple[(11, 57, 42, 78), (11, 27, 42, 97), (11, 6, 42, 16), (8, 97, 42, 52β¦
99 β Tuple[(11, 48, 99, 48), (8, 96, 99, 51), (11, 3, 99, 30), (8, 23, 99, 30)β¦
julia> @btime group(t->t[3], t->t[1] β [11,8,3] ? () : t, A )
5.605 ms (100637 allocations: 16.33 MiB)
100-element Dictionary{Int64, Vector{Union{Tuple{}, NTuple{4, Int64}}}}
4 β Union{Tuple{}, NTuple{4, Int64}}[(11, 35, 4, 28), (), (), (), (), (), (), β¦
96 β Union{Tuple{}, NTuple{4, Int64}}[(), (), (), (), (), (), (), (), (), () β¦β¦
95 β Union{Tuple{}, NTuple{4, Int64}}[(), (), (), (), (), (), (), (), (), () β¦β¦
63 β Union{Tuple{}, NTuple{4, Int64}}[(), (), (), (), (), (), (), (), (), () β¦β¦