16

In XQuery, How do you order by ascending and descending?

I have got the following from a tutorial:

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title

would it be

for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title ascending
return $x/title
Kindle Q
  • 944
  • 2
  • 19
  • 28
Tuffy G
  • 1,521
  • 8
  • 31
  • 42

2 Answers2

21

Yes, you can use ascending (default) or descending at the end of the order by.. expression.

Here's the link to the relevant part of the W3C XQuery spec:

http://www.w3.org/TR/xquery/#doc-xquery-OrderSpec

modulitos
  • 14,737
  • 16
  • 67
  • 110
Lucero
  • 59,176
  • 9
  • 122
  • 152
  • so it would befor $x in doc("books.xml")/bookstore/book where $x/price>30 order by ascending $x/title return $x/title – Tuffy G May 14 '11 at 11:04
  • or would it be for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title ascending – Tuffy G May 14 '11 at 11:16
  • I wrote "after the expression", which I meant like this: `... order by $x/title ascending ...` (just like you wrote in your question, this is the correct syntax). – Lucero May 14 '11 at 11:23
1

See my answer to this question. The code demonstrates ordering in descending order.

For ordering in ascending order, the ascending keyword can be ommitted.

Community
  • 1
  • 1
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431