Back to top

ReQL command: orderBy

Command syntax

table.orderBy([key | function]).optArg("index", index) → selection<stream>

selection.orderBy(key | function[, ...]) → selection<array>

sequence.orderBy(key | function[, ...]) → array

Description

Sort the sequence by document values of the given key(s). To specify the ordering, wrap the attribute with either r.asc or r.desc (defaults to ascending).

Note: RethinkDB uses byte-wise ordering for orderBy and does not support Unicode collations; non-ASCII characters will be sorted by UTF-8 codepoint. For more information on RethinkDB’s sorting order, read the section in ReQL data types.

Sorting without an index requires the server to hold the sequence in memory, and is limited to 100,000 documents (or the setting of the arrayLimit option for run). Sorting with an index can be done on arbitrarily large tables, or after a between command using the same index. This applies to both secondary indexes and the primary key (e.g., {"index": "id"}).

Sorting functions passed to orderBy must be deterministic. You cannot, for instance, order rows using the random command. Using a non-deterministic function with orderBy will raise a ReqlQueryLogicError.

Example: Order all the posts using the index date.

r.table("posts").orderBy().optArg("index", "date").run(conn);

The index must either be the primary key or have been previously created with indexCreate.

r.table("posts").indexCreate("date").run(conn);

You can also select a descending ordering:

r.table("posts").orderBy().optArg("index", r.desc("date")).run(conn);

Example: Order a sequence without an index.

r.table("posts").get(1).g("comments").orderBy("date").run(conn);

You can also select a descending ordering:

r.table("posts").get(1).g("comments").orderBy(r.desc("date")).run(conn);

If you’re doing ad-hoc analysis and know your table won’t have more then 100,000 elements (or you’ve changed the setting of the array_limit option for run) you can run orderBy without an index:

r.table("small_table").orderBy("date").run(conn);

Example: You can efficiently order using multiple fields by using a compound index.

Order by date and title.

r.table("posts").orderBy().optArg("index", "date_and_title").run(conn);

The index must either be the primary key or have been previously created with indexCreate.

r.table("posts").indexCreate("date_and_title",
    post -> r.array(post.g("date"), post.g("title"))
).run(conn);

Note: You cannot specify multiple orders in a compound index. See issue #2306 to track progress.

Example: If you have a sequence with fewer documents than the arrayLimit, you can order it by multiple fields without an index.

r.table("small_table").orderBy("date", r.desc("title")).run(conn);

Example: Notice that an index ordering always has highest precedence. The following query orders posts by date, and if multiple posts were published on the same date, they will be ordered by title.

r.table("post").orderBy("title").optArg("index", "date").run(conn);

Example: Use nested field syntax to sort on fields from subdocuments. (You can also create indexes on nested fields using this syntax with indexCreate.)

r.table("user").orderBy(user -> user.g("group").g("id")).run(conn);

Example: You can efficiently order data on arbitrary expressions using indexes.

r.table("posts").orderBy().optArg("index", "votes").run(conn);

The index must have been previously created with indexCreate.

r.table("posts").indexCreate("votes",
    post -> post.g("upvotes").sub(post.g("downvotes"))
).run(conn);

Example: If you have a sequence with fewer documents than the arrayLimit, you can order it with an arbitrary function directly.

r.table("small_table").orderBy(
    doc -> doc.g("upvotes").sub(doc.g("downvotes"))
).run(conn);

You can also select a descending ordering:

r.table("small_table").orderBy(
    r.desc(doc -> doc.g("upvotes").sub(doc.g("downvotes")))
).run(conn);

Example: Ordering after a between command can be done as long as the same index is being used.

r.table("posts")
 .between(r.time(2013, 1, 1, "+00:00"), r.time(2013, 1, 1, "+00:00"))
 .optArg("index", "date")
 .orderBy().optArg("index", "date")
 .run(conn);

Get more help

Couldn't find what you were looking for?