Lists do not preserve order

The problem is that when I create lists of [string] type, the order of elements in Dgraph is different than my input.

Schema

<fruits>: [string] .

Example mutation

{
   "set":[
      {
         "uid":"_:root",
         "fruits":[
            "1",
            "a",
            "3",
            "2"
         ]
      }
   ]
}

Example query

{
 q(func: uid(0x2712a)) {
    fruits
  }
}

Response

{
  "data": {
    "q": [
      {
        "fruits": [
          "2",
          "1",
          "3",
          "a"
        ]
      }
    ]
  },
...

How should I store lists of which order is important to me?

There are two methods for ordered data:

  1. Store it as a JSON string in a single field
  2. Make a complex schema to track order

Here is some reading material:

4 Likes

Thanks @amaster507, I ended up building custom type for my object with order: int predicate to keep track of things.

I’m using a facet to track order because I may put the same node in multiple lists. It mostly* works, and you can order a query result by a facet.

* It may be slightly bugged in 20.07 when using lists inside nodes inside lists, but I haven’t nailed that down yet.