3
votes

Is there a way to specify where to place null values in a sorted list?

I have a list I'd like to sort in descending order. I want nodes with date=null to be included, just at the end of the list.

Looking for something like this in cypher:

ORDER BY date DESC NULLS LAST

From Neo4j:

When sorting the result set, NULL will always come at the end of the result set for ascending sorting, and first when doing descending sort.

http://neo4j.com/docs/developer-manual/current/#order-by-ordering-null

3
Could you select where not null, order that and then union select where null? It's a bit wordy but...Ant P
I could but that would be unnecessarily costly. There are only a number of nodes with the date property as null, out of about 5000 in total. Basically, only those that go to the last pages would see them.lifwanian

3 Answers

3
votes

You can sort by result of COALESCE.

For example we have the following nodes:

MERGE (A1:Test {name:'a1'}) 
MERGE (A2:Test {name:'a2', date: 1})
MERGE (A3:Test {name:'a3', date: 2})
MERGE (A4:Test {name:'a4'})

We can sort the following manner:

MATCH (A:Test)
RETURN A.name, 
       A.date 
ORDER BY 
       COALESCE(A.date, -1) DESC
2
votes

You can use following : ORDER BY date IS NULL DESC

0
votes

Perhaps use coalesce() in your ORDER BY?

ORDER BY coalesce(val, 0)