There is a list with objects. It is necessary to sort this list from different situations by different values of the class fields. Example:
public static void doSort(List<Mix> array, int start, int end, int type) { if (start >= end) return; int i = start, j = end; int cur = i - (i - j) / 2; while (i < j) { switch (type) { case 1: { while (i < cur && (array.get(i).getCost() <= array.get(cur).getCost())) { i++; } while (j > cur && (array.get(cur).getCost() <= array.get(j).getCost())) { j--; } } break; case 2: { while (i < cur && (array.get(i).getBaseCost() <= array.get(cur).getBaseCost())) { i++; } while (j > cur && (array.get(cur).getMaxSpeed() <= array.get(j).getMaxSpeed())) { j--; } } break; } if (i < j) { Collections.swap(array,i,j); if (i == cur) cur = j; else if (j == cur) cur = i; } } doSort(array, start, cur, type); doSort(array, cur+1, end, type); } Depending on the parameter passed, sorting is performed according to one or another field, the field is selected using switch.
Is it possible to get away from switch? Is sorting more flexible (via parameterized method, overload) in this case?