home contribute faq download

FunctX XQuery Functions

functx:sort-as-numeric

Sorts a sequence of numeric values or nodes

Google
Webxqueryfunctions.com

Description

The functx:sort-as-numeric function sorts a sequence of numeric values or nodes. This is useful for untyped values, because the order by clause by default treats values as strings, and would therefore sort 100 before 99.

Arguments and Return Type

NameTypeDescription
$seq item()* the sequence to sort
return value item()*

XQuery Function Declaration

See XSLT definition.
XQuery Syntax for July 2004 - January 2007 (1.0):
declare namespace functx = "http://www.functx.com"; 
declare function functx:sort-as-numeric 
  ( $seq as item()* )  as item()* {
       
   for $item in $seq
   order by number($item)
   return $item
 } ;
XQuery Syntax for May 2003:
declare namespace functx = "http://www.functx.com" 
define function sort-as-numeric 
  ( $seq as item()* )  as item()* {
       
   for $item in $seq
   order by fn:number($item)
   return $item
 }

Examples

let $in-xml :=
<in-xml>
  <f>1</f>
  <f>35</f>
  <e>4</e>
</in-xml>
return
XQuery ExampleResults
functx:sort-as-numeric(('1','100','99'))
('1', '99', '100')
functx:sort-as-numeric($in-xml/*)
<f>1</f>
<e>4</e>
<f>35</f>

See Also

functx:sortSorts a sequence of values or nodes
functx:sort-case-insensitiveSorts a sequence of values or nodes regardless of capitalization
functx:sort-document-orderSorts a sequence of nodes in document order

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Need XQuery Help?
D A T Y P I C
Training | Consulting | Development

XQuery by Priscilla WalmsleyGet the book!
XQuery by Priscilla Walmsley