there might be a way using some sort of clever sorting magic trick, but if these are lists of numbers, it would be quite easy to write a little VB script for it as well.
Can you give a very specific example of the input you have and the output you desire?