Off the top of my head again, what if you concatenate the numbers?

510512
511512

No matter what you have in the last 3 positions, 510 will always be in front of 511.

Robert