In general to do sorting then grouping you need to do a two-phase transformation. You can do this using two stylesheets, or using a single stylesheet if you write the result of the first phase to a variable and then (assuming XSLT 1.0) convert this variable to a node-set using the xx:node-set() extension function. This means that after sorting you need to copy the sorted records to the intermediate tree using xsl:copy-of; the preceding-sibling axis on the copied nodes will reflect their position in the intermediate tree. Remember that axes always reflect relationships among the nodes in a tree: sorting only changes the processing order of the nodes, it doesn't modify the original tree.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference