Using ORDER BY ASC on a string with number

Hi guys,

Say I have the following results that comes from two variables in my SPARQL query:

?wall ?ID
:wall1 “AB21”
:wall2 “AB14”
:wall3 “AB2”
:wall4 “AB3”

When I use ORDER BY ASC(?ID) I get the following:

?wall ?ID
:wall1 “AB14”
:wall2 “AB2”
:wall3 “AB21”
:wall4 “AB3”

Since I think it only looks for the next item in the string (i.e. 1, 2, 21, 3), but is there a way to treat it as an integer or make it look at the full number?

You’ll probably need to take the integer suffix of ?ID bindings to a separate variable (using xsd:integer for casting), then order by that one. Strings are sorted lexicographically.

Cheers,
Pavel

Is there any way to define a custom sort order for a user defined datatype? Plugin perhaps? Just curious.

Not directly, but one can have a user-defined function which maps values of custom datatypes to integers… That’d be another way to solve the topic starter’s problem but perhaps an overkill here.

Cheers,
Pavel

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.