# Filter and reorder pairs of elements?

Hi everybody, this is an algorithmic problem:
i have a set of pairs of elements, from the same group,the members of the group are integers, the first member of a pair is an element of the group, and the second one is a group of elements of the same mother group. distinct from the first member of the pair

``````group = 1:9 #this element contains all possible unique elements
pairs = ((1,[2,3,4]),
(2,[5,7,8]),
(5,),
(6,)
``````

I’m looking for a function to reorder and filter the first element of the group and the second members of the pairs, an example:

``````subgroup= [8,5,1,2]
pairs = ((1,[2,3,4]),
(2,[5,7,8]),
(5,),
(6,)
julia>reorder!(pairs,subgroup)
pairs =  ((5,)
(1,)
(2,[8,5]))
``````

there is a function that can do that in julia? or the solution is a loop of loops? there is no need for optimizations, as the number of elements is low. there is a function that does something similar, but just for a vector:

``````group = 1:21
pairs= 1:3
subgroup = [3,1,5]
julia> intersect(subgroup,pairs)
2-element Array{Int64,1}:
3
1
``````

Sorry, I find the description of the problem hard to understand.

It is unlikely that there is a function specifically for this, so you would have to build from existing parts. `sort`, with `lt` and `by` arguments, should be useful, and also `filter`. Alternatively, if you can write your algorithm using loops, that should be fine too.

1 Like

Yes, I think I have a solution now. First I intersect see branches, then I reorder the first level and finally I reconstruct the structure. I’ll post it here when I write it, basically is an alternate way to iterate over few elements of a matrix, and the numbers are the indices, but the order or amount of those indices can vary over time, I’m trying to do the equivalent of:
A=rand(21,21)
n=[3, 2,5]
B=A[n,n]
But with a tree, I learned