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?
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
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
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?
besides, can I use unwind and make the value used in the where condition?
@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.
I couldn't understood your second question. Could you please create a example and ask?
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.
@oli I couldn't find a way to sort in descending order. But, you can check an apoc.iterate function.
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!