Search code examples
sparqlrdftriplestoregraphdb

Ascending order of results in sparql by using nonnegative integers that come as strings


I have more than 2000 resources in my triple store (GraphDB) that I would like to sort in ascending order, based on their IRI, whereas the IRI looks like:

http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#3
...
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2345

In my graph, all abovementioned IRIs are connected to one resource by "DE6:complex_Data_Type_has_Member". In order to get all the abovementioned IRIs I do the following:

PREFIX DE6: <http://DE6/DINEN61360#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?IRIs_to_Sort WHERE{
         ?Array a DE6:Data_Element;
         DE6:complex_Data_Type_has_Member ?IRIs_to_Sort .


} ORDER BY ?IRIs_to_Sort 

If I just use the "ORDER BY ?IRIs_to_Sort" solution sequence modifier, where as shown in https://www.w3.org/TR/2013/REC-sparql11-query-20130321/#modOrderBy

then I get the following result (only first ten reults are shown):

http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#10
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#100
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1000
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1001
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1002
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1003
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1004
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1005
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1006
...
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1999
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#20
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#200
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2000
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2001
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2002
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2003
...

Instead, I would expect something like:

http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#1
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#3
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#4
...
http://TABLE/240b9f63-66b9-47f1-9e02-bfd03794bbd9#2099

What am I missing here?


Solution

  • IRIs are treated as lexical values for ORDER BY, which means that the numbers in your fragment ids are treated as text, and they are not considered on their own.

    A full ORDER BY that I think will work for you, whether your result set includes one or many IRIs with one or many frag-IDs each, but only if all frag-IDs are numeric, would be --

    ORDER BY ( xsd:string ( STRBEFORE ( STR ( ?IRIs_to_Sort ), "#" ) ) )
             ( xsd:long ( STRAFTER ( STR ( ?IRIs_to_Sort ), "#" ) ) ) 
    

    If your frag-IDs are a blend of numeric and alphanumeric, things get rather more complicated.