How to Order Results in WOQL
To use this HowTo, first clone the Star Wars demo into your team on TerminusCMS. You will then have access to the data needed for this tutorial.
Ordering results using order_by
The order_by
keyword will allow you to sort results.
let v = Vars("person", "label", "eyes", "group");
limit(2)
.order_by(["eyes", "desc"])
.select(v.eyes, v.group)
.group_by(
"eyes",
["label"],
v.group,
and(triple(v.person, "rdf:type", "@schema:People"),
triple(v.person, "label", v.label),
triple(v.person, "eye_color", v.eyes)))
This returns the first two results of people, who have a given eye color, sorted by eye color, in reverse order.
To get the alternative order, you can write:
let v = Vars("person", "label", "eyes", "group");
limit(2)
.order_by(["eyes", "asc"])
.select(v.eyes, v.group)
.group_by(
"eyes",
["label"],
v.group,
and(triple(v.person, "rdf:type", "@schema:People"),
triple(v.person, "label", v.label),
triple(v.person, "eye_color", v.eyes)))
Or simply:
let v = Vars("person", "label", "eyes", "group");
limit(2)
.order_by("eyes")
.select(v.eyes, v.group)
.group_by(
"eyes",
["label"],
v.group,
and(triple(v.person, "rdf:type", "@schema:People"),
triple(v.person, "label", v.label),
triple(v.person, "eye_color", v.eyes)))