sorting before comparison