I have a switch statement that based on the user's input will sort an array based on array[i].name or sort by array[i].mark
switch(input){
case 1:
for(int i = 1; i < size; i++){
structure_name choice = Array[i];
int j = i - 1;
while(j >= 0 && tolower(choice.name[0]) <= tolower(Array[j].name[0])){
Array[j + 1] = Array[j];
j = j - 1;
}
Array[j+1] = choice;
}
break;
case 2:
for(int i = 1; i < size; i++){
structure_name choice = Array[i];
int j = i - 1;
while(j >= 0 && choice.mark <= Array[j].mark){
Array[j + 1] = Array[j];
j = j - 1;
}
Array[j+1] = choice;
}
break;
default:
break;
}
As you can see, I'm performing a selection sort twice. Is it possible to perform something like...
if(input == 1){
option = .name
}else if(input == 2){
option = .mark
}
So that I can use just one sort and change what variable it's taking from the array. Note: I cant use sort(), I have to use the sort algorithm I've written out. Hence the dilemma of me trying to avoid writing essentially the same code twice.