Syntax for returning distinct nodes

So here's my dilemma...

I want to be able to find distinct pairs of node with some value (a: nodeA {number: 1}), that are found in node (b)
by distinct pairs I mean:
1,2
3,4

and not:
1,2
2,1

I'm just looking for the proper syntax so I can implement it in my code.

Hello @Shukaku :slight_smile:

This is a way of doing this, with APOC you can also sort list of nodes if you need more data from them:

WITH [[1,2], [2,1], [3,4], [4,3], [5,6]] AS list
UNWIND list AS items
WITH apoc.coll.sort(items) AS items_sort, collect(items)[0] AS items_list
RETURN items_list

Regards,
Cobra

What if I'm unable to call apoc.coll.sort? is there an alternative to that?

To be honest, I'm curious if you find something only in Cypher :slight_smile: I had this problem a few days ago and I only found this solution with APOC.

For distinct node pairs, once you have the variables for both nodes, you can use the following to get rid of mirrored results:

...
WITH n1, n2
WHERE id(n1) < id(n2)
...
1 Like

Thank you @andrew.bowman and @Cobra!
Andrew's answer is what I was looking for.

1 Like