In some implementations you can experience that the order of the result in more complex FLOWR expressions is arbitrary, for instance taking an example from Priscilla Walmsley's book http://www.datypic.com/books/xquery/ a grouping of item
elements ('order.xml' in http://www.datypic.com/books/xquery/chapter01.html) with
<order num="00299432" date="2015-09-15" cust="0221A">
<item dept="WMN" num="557" quantity="1" color="navy"/>
<item dept="ACC" num="563" quantity="1"/>
<item dept="ACC" num="443" quantity="2"/>
<item dept="MEN" num="784" quantity="1" color="white"/>
<item dept="MEN" num="784" quantity="1" color="gray"/>
<item dept="WMN" num="557" quantity="1" color="black"/>
</order>
in Saxon 9.8 HE with the code (7.11 in http://www.datypic.com/books/xquery/chapter07.html)
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method 'xml';
declare option output:indent 'yes';
for $item in //item
group by $d:= $item/@dept, $n:= $item/@num
return <group dept="{$d}" num="{$n}" count="{count($item)}"/>
gives the output
<?xml version="1.0" encoding="UTF-8"?>
<group dept="ACC" num="563" count="1"/>
<group dept="MEN" num="784" count="2"/>
<group dept="WMN" num="557" count="2"/>
<group dept="ACC" num="443" count="1"/>
while for instance with BaseX you might get a different output order (I think based on the input order of items if I remember it correctly) for the group
elements, for instance in BaseX 9 I get
<group dept="WMN" num="557" count="2"/>
<group dept="ACC" num="563" count="1"/>
<group dept="ACC" num="443" count="1"/>
<group dept="MEN" num="784" count="2"/>