# Lists do not preserve order

**URL:** <https://discuss.dgraph.io/t/lists-do-not-preserve-order/10616>\
**Category:** Issues\
**Created:** [September 26, 2020, 8:41am UTC](https://discuss.dgraph.io/t/lists-do-not-preserve-order/10616 "2020-09-26T08:41:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AugustDev](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@AugustDev](https://discuss.dgraph.io/u/AugustDev)\
**Post date:** [September 26, 2020, 8:41am UTC](https://discuss.dgraph.io/t/lists-do-not-preserve-order/10616/1 "2020-09-26T08:41:02Z")

</div>

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

Schema

```auto
<fruits>: [string] .

```

Example mutation

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

```

Example query

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

```

Response

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

```

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

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [September 26, 2020, 9:39am UTC](https://discuss.dgraph.io/t/lists-do-not-preserve-order/10616/2 "2020-09-26T09:39:53Z")

</div>

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:

> [@Documentation that arrays work as a set not storing duplicates](http://discuss.hypermode.com/t/documentation-that-arrays-work-as-a-set-not-storing-duplicates/9590):
>
> Continuing the discussion from [Schema for maps of kv pairs with variant values](http://discuss.hypermode.com/t/schema-for-maps-of-kv-pairs-with-variant-values/9569/7): [https://dgraph.io/docs/query-language/schema/#list-type](https://dgraph.io/docs/query-language/schema/#list-type) A set operation adds to the list of values. The order of the stored values is non-deterministic. A delete operation deletes the value from the list. Querying for these predicates would return the list in an array. Indexes can be applied on predicates which have a list type and you can use [Functions](https://dgraph.io/docs/query-language/schema/#functions) on them. Sorting is not allowed using these predicates. Cla…

> [@Is the Order of Arrays Preserved?](http://discuss.hypermode.com/t/is-the-order-of-arrays-preserved/9251):
>
> I know arrays are handled like sets in Dgraph and duplicates are not preserved, but is the order of the set preserved or is it possible that they could be reordered by the dgraph engine? What I want to ensure is that I can do this: type Task { orderedList: [String] } Example, If I add a value as ["b","c","a"] is it ever possible that it could be returned in a different order such as ["a","b","c"] I have a field that needs to have an ordered list and graphql cannot handle that very well nat…

> [@Feature Request: Ordered Arrays](http://discuss.hypermode.com/t/feature-request-ordered-arrays/9644):
>
> Continuing the discussion from [Schema for maps of kv pairs with variant values](http://discuss.hypermode.com/t/schema-for-maps-of-kv-pairs-with-variant-values/9569/10): It would be nice if there was a way to store an ordered set without a messy or concatenating a string to reorder in a UI. Dgraph supports [String] but it is somewhat uncommon knowledge that they work like a set with one gotcha (order) Dgraph arrays do not store duplicates Dgraph arrays do not store order This is understandable given the API generated. Removing by value could potentially remove more than one, if …

> [@How to store ordered data?](http://discuss.hypermode.com/t/how-to-store-ordered-data/6269):
>
> I am new to graph database, and I’m wondering how to store ordered data in graph database. I have a blog project, and each series has many ordered articles like the first article of this series, the second article of this series. My goal is: For each series, I can find all its articles with their order preserved For each article, I can find its series, and its previous and next article I have two solutions in mind: Give each article a series\_index to store its order in the series Give eac…

> [@String array out of order](http://discuss.hypermode.com/t/string-array-out-of-order/9008):
>
> Very new user of Dgraph here but so far, loving it! I’ve encountered a really strange bug(?) it seems In my schema I have: Product.facts [string] const createProduct = new Mutation(); const x = { "dgraph.type": "Product", "Product.attributes": "{\"string\":\"42\",\"number\":42,\"nested\":{\"nested\":\"property\"},\"boolean\":false}", "Product.brand": { "uid": "0x24f" }, "Product.facts": ["test fact #1", "test fact #2", "test fact #3"], …

> [@GraphQL+- -\> Spec Compliant GraphQL](http://discuss.hypermode.com/t/graphql-spec-compliant-graphql/5891/5):
>
> This has been requested before, but since we’re aiming for spec compliant GraphQL, I thought I’d put this here. Lists in Dgraph behave like an unordered set, which makes perfect sense from a graph point of view, but they do not suit all types of data. The [GraphQL spec](https://spec.graphql.org/draft/#sel-GAFdVFABABbruc) says that “lists are ordered sequences of values”, and I think we should aim for that. In DQL, if we want to use unordered lists for performance benefits, perhaps we could create a directive to guarantee order?

---

<div class="post-metadata">

**Author:** ![AugustDev](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@AugustDev](https://discuss.dgraph.io/u/AugustDev)\
**Post date:** [September 26, 2020, 9:48am UTC](https://discuss.dgraph.io/t/lists-do-not-preserve-order/10616/3 "2020-09-26T09:48:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dpk](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dpk](https://discuss.dgraph.io/u/dpk)\
**Post date:** [January 6, 2021, 1:01am UTC](https://discuss.dgraph.io/t/lists-do-not-preserve-order/10616/4 "2021-01-06T01:01:54Z")

</div>

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.
