2016-02-21 70 views
0

我想在question中執行與用戶非常相似的操作,即排序集合中元素的結果。但是,與該問題的用例不同,我的收藏是未排序的。Neo4j Cypher排序無序集合

所以使用例如,從這個問題:

```MATCH (node:node) - [:has] -> (scores) 
WITH node 
ORDER BY node.created DESC 
RETURN count(node.name) AS count, node.name, collect(node.created) AS created 

count node.name created 
3  "name1"  [1410234609620,1410234606534,1410234506799] 
1  "name2"  [1410234434936] 
1  "name3"  [1410234454573] 
2  "name4"  [1410463902552,1410460561481] 
1  "name5"  [1410234485185] 
2  "name6"  [1410234548527,1410234525740] 
``` 

接受的答案是:

MATCH (node:node) 
WITH node 
ORDER BY node.created DESC 
WITH count(node.name) AS count, node.name as name, collect(node.created) AS created 
WITH count, name, head(created) as timeSort, created 
RETURN count, name, created 
ORDER BY timeSort DESC 

獲得頭(創建)返回集合時已經排序正確的結果。我的不是。在打電話給頭之前有沒有辦法對這個集合進行分類?

回答

1

排序集合,你可以使用UNWIND + ORDER BY

UNWIND coll as x 
WITH x ORDER BY x.foo 
RETURN collect(x); 
+0

我不認爲平倉是我需要的,因爲在上面的代碼可以收集之前通過向運行順序。在代碼中,node.created返回一個時間戳列表。我需要對這些時間進行排序,以便我能夠獲得最近的時間,然後我想按照該列表的頭部排序節點。 – monjohn

+0

如果您想對其進行分類,則需要取消收集。或在收集之前對數據進行分類。 –