Say I have the following documents in my database: a\_doc1, a\_doc2, b\_doc1, and b\_doc2

All these documents are of the following format

```
<doc>
  ....
  <updatedTime>2011-02-07T14:41:02.133-05:00</updatedTime>
  ....
</doc

```

>

The value of the "updatedTime" element is inserted when the document is created using the fn:current-dateTime()

Now I am trying to do the following:

1. find all documents whose name starts with "a\_"
2. order these documents by their `<updatedTime>` element in descending order
3. Return the first document name from the descending order

I tried the following:

```
for $doc_name in db:list()
  where fn:starts-with($doc_name, 'a_')
    order by xs:dateTime(doc($doc_name)/updatedTime) descending
      return $doc_name

```

Say "a\_doc1" is created at **"2011-02-07T14:40:00.78-05:00"** and "a\_doc2" is created at **"2011-02-07T14:41:02.133-05:00"**, the desired output is **a\_doc2**. In short the document name(starting with a\_) of the **most recent document** created must be returned.

When I try my sample code, the output returned is : [a\_doc1, a\_doc2]. 
The expected output is: [a\_doc2, a\_doc1]. 

Thanks,
Sony