Refactoring in sorting function for sorting contact pairs by similarity function.