1

When creating a map from collections via apoc's apoc.map.fromLists (or plain cypher for all I care), is there any way to respect/keep the sorting? Or is it just "It's a map, sorting doesn't make sense anyway".

specifically, when executing

with [{name: "z", a: 1}, {name: "b", b: 1}, {name: "b2", b: 2}] as qs
return apoc.map.fromLists([q in qs | q.name], qs)

I get

{
  "b2": {
    "name": "b2",
    "b": 2
  },
  "z": {
    "name": "z",
    "a": 1
  },
  "b": {
    "name": "b",
    "b": 1
  }
}

where indeed I would like to have

{
  "z": {
    "name": "z",
    "a": 1
  },
  "b": {
    "name": "b",
    "b": 1
  },
  "b2": {
    "name": "b2",
    "b": 2
  }
}

1 Answer 1

1

Sorry, maps in Cypher are unsorted and unsortable structures, with regard to map keys.

There is apoc.maps.sortedProperties(), however this doesn't return a map, but a list of key/value pairs, where those pairs are sorted alphabetically.

2
  • I see, so there is no way to control the sorting of maps other than alphabetical keys?
    – tscherg
    Jul 16, 2018 at 14:16
  • Right, there is no concept in the map structures for ordering of keys. You would need to transform it to a non-map structure which could be sorted, such as the APOC function provided. Jul 16, 2018 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.