Performing Natural sort

Hi,

We have question regarding sorting in Stardog as follows.

ORDER BY statements in SPARQL queries perform alphanumerical sorts like this:

However, I was wondering if we can somehow change (idk maybe through UDF etc) the behaviour of ORDER BY in Stardog query processing to get natural sort (Natural sort order - Wikipedia) ,eg, something like this:
image

We're using stardog-spring and snarl template to execute our queries.

Hi Cesar,

you could extract the numeric values and assign them to a new variable by which the results can be sorted. For example:

BIND(xsd:integer(REPLACE(?var, "[^0-9.]", "")) AS ?id)

And then just order by ?id.

Best regards
Lars

1 Like

Nice! Thank you Lars.
Just a follow up question. The given example in the question was a simple one to better explain it.

Let assume that we want to introduce some priorities or more complex conditions to sort the content. I guess one way to do it is to first extract the content from the db (eg, through a SELECT query with no limit) and then perform some post processing in the application, in our case in Java. I would imagine that this approach may be expensive when the result cardinality of the SELECT query is large. I am now curious to know if it is possible to introduce kind of of built-in function/UDF to use it inside SPARQL in Stardog v.9 to do it without the need to do the post processing in in our application.

Hi Cesar,

sure this is also possible. You can find the documentation for UDFs here. In addition, you can also checkout the stardog-extensions as a reference to help you get started. (Note that the stardog-extensions repo is not officially maintained by Stardog).

Best regards
Lars

1 Like