rethinkdb / docs

RethinkDB documentation
http://rethinkdb.com/docs
Apache License 2.0
117 stars 167 forks source link

Document sorting order of ReQL types #734

Closed danielmewes closed 9 years ago

danielmewes commented 9 years ago

We've had many questions about how different ReQL types sort relative to each other, and also within one type.

For example it's not necessarily obvious that arrays sort lexicographically, and that null sorts between some other types (and not for example lower than all other types).

We should explain this somewhere.

danielmewes commented 9 years ago

Sorting order of objects is another non-obvious one (I don't know how they sort actually. @mlucy?)

mglukhovsky commented 9 years ago

The data types document might be a good place to document this.

mlucy commented 9 years ago

@danielmewes -- obj1 <=> obj2 == obj1.coerce_to('array') <=> obj2.coerce_to('array'). We order the fields lexicographically and compare one by one, considering the absence of a key to be smaller than any value for a key.

chipotle commented 9 years ago

So this explains how arrays elements are sorted and how object fields are sorted, but the other question is how types sort relative to one another -- if I have a sequence of [100, 200, "a", "b", [1, 2, 3], {"foo": "bar", "count": 999}, null], what happens?

mlucy commented 9 years ago

Values of different types sort in the same order as their type names. (So if x and y are different types, x <=> y == x.type_of() <=> y.type_of().)