MATCH (N0:user)
OPTIONAL MATCH (N0:user)-[R0:rating]->()
WITH distinct N0,R0, N0.age as AN0, count(R0.likes) as AR0
order by ID(N0), AN0, AR0 desc
WITH N0.age as O1, head(collect(R0.likes)) as O2, N0
RETURN O1, O2, count(ID(N0))
ORDER BY O1, O2
But I don't know why if I change the order of variables (N0,R0,..) after the clause DISTINCT this change the result of the query