using custom merge sort to avoid transitivity exception