cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to loop over a list and collect the processed value

oli
Graph Buddy

Suppose I collect two list from last query L1 and L2, L1 is [8, 9, 5, 3] and L2 is [4, 7, 1] and I wish to get the one step bigger item in L1 comparing with L2, so the expected output is [5, 8, 3]. How could cypher do this?

9 REPLIES 9

oli
Graph Buddy

Got half of the answer, when I query with following, it shows

with [4, 7, 1] as L2, [8, 9, 5, 3] as L1
unwind L2 as l2
    with [x in L1 where x > l2] as cl
    return cl

3X_d_1_d148077d2ad9ec28d8fee794d2aaa9d6f648321a.png
That's the half way and what I need to do is only to take the minimum value of from each list and got the final result, therefore I add a min() function

with [4, 7, 1] as L2, [8, 9, 5, 3] as L1
unwind L2 as l2
    with [x in L1 where x > l2] as cl
    return min(cl)

it returns
3X_9_4_9405bb11141517d45e96e8f378fd26b413125b86.png
It seems that it returns me the min value in the wrong dim

Hey @oli ,
You can try apoc.coll.sort() function to solve this problem. I have written the code as well you can run it and test it.

with [4, 7, 1] as L2, [8, 9, 5, 3] as L1

unwind L2 as l2

    with [x in L1 where x > l2] as cl

with apoc.coll.sort(cl) as cl

with collect(cl[0]) as cl

return cl

Thanks,
Rishabh

Thanks prishabh for your answer, do you know how could I set the order of asc or desc?

@oli you can use the ORDER BY x.abc ASC or DESC. But in your scenario, it is creating a list of list s it is sorting only the list not values inside the list.

Yeah, I know. since apoc.coll.sort() just sort the value in asc order, that's why I would to ask how to sort the list in desc order. For the second question, I got the answer already, it's about how to unwind and save the value locally instead of globally.

besides, can I use unwind and make the value used in the where condition?

I couldn't understood your second question. Could you please create a example and ask?

Hi,

Just chiming in because this answer was super helpful to me. I'm facing a very similar problem right now and couldn't find a solution off the top of my head.

Thank you and the community!

@oli I couldn't find a way to sort in descending order. But, you can check an apoc.iterate function.