An XQuery FLWOR expression with an order by clause may fail with a message such as
XPTY0004: Non-comparable types found while sorting:
net.sf.saxon.value.Int64Value cannot be cast to net.sf.saxon.value.StringValue
There are two necessary conditions: (a) the data type of the sort key must be something other than string or untypedAtomic, and (b) the FLWOR expression must not be at the outermost level of the query or of a function body.
A patch is being placed in Subversion.