The default order of groups is "order of first appearance" - i.e. the groups are ordered according to the position of their first item in the original population.
To sort the groups according to the value of the grouping key, use
<xsl:sort select="current-grouping-key()"/> immediately within <xsl:for-each-group>
To sort the items within each group, use an <xsl:sort> element as a child of the <xsl:apply-templates> instruction.
If you think you are getting results that don't follow the spec, it's useful to say what XSLT processor you are using.
Michael Kay
http://www.saxonica.com/
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference