Can you give us an example of what you mean by "rank" vs. "order"? Do you have sample output that can show us actual output vs. expected output? Can you show us any code that produces the above?
It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.