I'm looking for an algorithm to rank stock pair from a pool based on the difference of PE i.e PE stock 1 - PE stock 2
i.e pool of 40 stocks, rank based on unique stock pair based on smallest PE difference. Total will have 20 unique pairs
eg. MSFT appears in pair 1, with smallest PE associated with MSFT pair, MSFT should not reappear again in the subsequent pair
What's the correct algorithm for doing this?
So far I have tried finding PE difference for all stock in the universe and rank accordingly,however some stocks repeated a few times in the PE difference calculation