title: xquery order by with results from fn replace
body: <p>I'm ordering search results (from many XML files) according to the contents of the XML element . Tricky thing is I need to order the results according to only a part of the string found in this element, namely the number following the string 'Rep.'. For instance 'BPH, Rep. <strong>49</strong>, T I Nr. 22' should come before 'I. HA Rep. <strong>100</strong>, Nr. 2233'. I use regex to isolate the substring with fn:replace. The regex is definitely correct, it spits out the right number every time. For some reason it won't order correctly though. What am I missing? Thanks from a newbie.</p>
  
  <p>The XML looks like this:</p>
  
  <pre><code>&lt;xml&gt;
  &lt;idno&gt;I. HA Rep. 100, Nr. 2233&lt;/idno&gt;
  &lt;idno&gt;I. HA Rep. 100, Nr. 2535&lt;/idno&gt;
  &lt;idno&gt;BPH, Rep. 113, Nr. 1694&lt;/idno&gt;
  &lt;idno&gt;BPH, Rep. 113, Nr. 2845&lt;/idno&gt;
  &lt;idno&gt;BPH, Rep. 192 NL Wittgenstein, IV, 2, 14&lt;/idno&gt;
  &lt;idno&gt;BPH, Rep. 49, T I Nr. 21&lt;/idno&gt;
  &lt;idno&gt;BPH, Rep. 49, T I Nr. 22&lt;/idno&gt;
  &lt;idno&gt;(D) BPH, Rep. 48, Nr. 141&lt;/idno&gt;
  &lt;idno&gt;(D) BPH, Rep. 48, Nr. 144&lt;/idno&gt;
  &lt;idno&gt;BPH, Rep. 192, NL Wittgenstein, VI,9,7&lt;/idno&gt;
  &lt;idno&gt;I. HA Rep. 178 F, Nr. 31&lt;/idno&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>Xquery is like this:</p>
  
  <pre><code>  for $record in $records
    order by replace($record//idno, '(.*Rep\.\s)(\d+)(.*)', '$2')
    return $record//idno
  </code></pre>
  
  <p>The result:</p>
  
  <pre><code>I. HA Rep. 100, Nr. 2233
  I. HA Rep. 100, Nr. 2535 
  BPH, Rep. 113, Nr. 2845 
  BPH, Rep. 113, Nr. 1694 
  I. HA Rep. 178 F, Nr. 31 
  BPH, Rep. 192, NL Wittgenstein, VI,9,7 
  BPH, Rep. 192 NL Wittgenstein, IV, 2, 14 
  (D) BPH, Rep. 48, Nr. 141 
  (D) BPH, Rep. 48, Nr. 144 
  BPH, Rep. 49, T I Nr. 21 
  BPH, Rep. 49, T I Nr. 22 
  </code></pre>
  
  <p>Desired result:</p>
  
  <pre><code>(D) BPH, Rep. 48, Nr. 141
  (D) BPH, Rep. 48, Nr. 144
  BPH, Rep. 49, T I Nr. 21
  BPH, Rep. 49, T I Nr. 22
  I. HA Rep. 100, Nr. 2233
  I. HA Rep. 100, Nr. 2535
  BPH, Rep. 113, Nr. 1694
  BPH, Rep. 113, Nr. 2845
  I. HA Rep. 178 F, Nr. 31
  BPH, Rep. 192, NL Wittgenstein, VI,9,7
  BPH, Rep. 192 NL Wittgenstein, IV, 2, 14
  </code></pre>
  
  <p>If I change my 'order by' to something simple - like, ordering by a different element without using regex - it works. I can't figure out how it is ordering. We are talking about thousands of idnos, and I can't figure out the pattern it is supposedly ordering them by. I can add more  to the list though if someone needs more examples to figure it out. Thank you!!!!</p>