# Filter and reorder pairs of elements?

**URL:** <https://discourse.julialang.org/t/filter-and-reorder-pairs-of-elements/23128>\
**Category:** General Usage\
**Created:** [April 14, 2019, 2:31am UTC](https://discourse.julialang.org/t/filter-and-reorder-pairs-of-elements/23128 "2019-04-14T02:31:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 14, 2019, 2:31am UTC](https://discourse.julialang.org/t/filter-and-reorder-pairs-of-elements/23128/1 "2019-04-14T02:31:35Z")

</div>

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

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

```

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:

```julia
subgroup= [8,5,1,2]
pairs = ((1,[2,3,4]),
             (2,[5,7,8]),
             (5,[8]),
             (6,[8])
julia>reorder!(pairs,subgroup)
pairs = ((5,[8])
               (1,[2])
               (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:

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

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 14, 2019, 5:37am UTC](https://discourse.julialang.org/t/filter-and-reorder-pairs-of-elements/23128/2 "2019-04-14T05:37:17Z")

</div>

> [@longemen3000](#):
>
> 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:

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

> [@longemen3000](#):
>
> there is a function that can do that in julia? or the solution is a loop of loops?

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.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [April 14, 2019, 8:53am UTC](https://discourse.julialang.org/t/filter-and-reorder-pairs-of-elements/23128/3 "2019-04-14T08:53:24Z")

</div>

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
