Yes, the Asset Tracking example from InfoPath 2003 does have code to do this and you can leverage that in your solution.
Unfortuantely the InfoPath 2007 Asset Tracking solution does not have this because the design point for the InfoPath 2007 samples was to not use any code. Sorting items in InfoPath currently requires code.
You can get a copy of the IP2003 Asset Tracking solution from the Microsoft web site if you only have IP2007. It will work in IP2007 just fine. It is available here: http://office.microsoft.com/en-us/templates/TC010785081033.aspx?pid=CT101481361033
Shiraz Cupala / Microsoft InfoPath MVPQdabra® Software / Streamline data gathering to turn process into knowledge