sort by in xquery : problem with alphanumerical values

Hello!

I want to sort a document with sort by clause, but I’ve problem to have a good sorting. In my document wich look like this :

<documents>
  <document>
    <number>001</number>
  </document>
  <document>
    <number>002</number>
  </document>
  <document>
    <number>44</number>
  </document>
  <document>
    <number>333</number>
  </document>
</documents>



when I sort on number by doing this sort by (number), output is incorrect because 44 comes after 333 and I think it’s because 4 is after 3, but can I’ve 44 comes before 333?

thanks in advance

Nathaire

Hi Nathaire,

the result is correct, because without any additional type information (e.g. from a schema), XQuery will sort the values as strings (where “333” < “44”). If you want numerical sort, you have to give type information, either in the Tamino schema or in the query, e.g.
declare namespace xs = "http://www.w3.org/2001/XMLSchema"
let $doc:=

001


002


44


333



for $i in $doc/document
sort by (xs:integer(number)) return $i

Regards

Harald

Thanks, but I know that.

The problem is that my field number is and must be of a type string because some contracts (number is for this) can have a number like this : A-1, ZZ-33,…

Maybe it’s impossible with tamino to sort correctly because I can’t use xs:integer for the field with alphabetical values.

Am I right?

Maybe it’s impossible with tamino to sort correctly because I can’t use xs:integer for the field with alphabetical values.

True, you cannot cast a non-numerical contents to a numerical data type. It’s the XQuery spec that does not allow this.

Regards

Harald

you can use a different xschema.
for example yuo can write:


AA
333




AA
44



so you can order these documents first with alfapart an than with numerical part

good job!
Gibbo