Isabi
Isabi

Reputation: 151

SPARQL query to sort by property

Assume having the following triples in the triplestore, i.e. five resources that have both a "hierarchical structure" as well as a "horizontal order":

<kiwi> rico:isOrWasIncludedIn <fruits> .
<apple> rico:isOrWasIncludedIn <fruits> .
<plum> rico:isOrWasIncludedIn <fruits> .
<orange> rico:isOrWasIncludedIn <fruits> .
<banana> rico:isOrWasIncludedIn <fruits> .
<orange> rico:followsOrFollowed <plum> .
<banana> rico:followsOrFollowed <kiwi> .
<apple> rico:followsOrFollowed <orange> .
<plum> rico:followsOrFollowed <banana> .

How would I query the triplestore with SPARQL to return the resources that are included in <fruits> in correct order like:

<kiwi>
<banana>
<plum>
<orange>
<apple>

Upvotes: 0

Views: 182

Answers (1)

HES
HES

Reputation: 157

To isolate those iris you can write a query like this:

PREFIX rico: <long form goes here>

SELECT DISTINCT ?iri WHERE 

{

?iri rico:isOrWasIncludedIn <fruits> .

}

I think I understand your request for order by. You could use a sub-query and bind a sequence of numbers to the entities in order - 1,2,3 etc. Then order by this new variable.

Upvotes: 1

Related Questions