I'm trying to do a project... The project needs to read from a csv file that have contacts. The contacts have several fields that I read to an array of structs of type contact. Ex: array.contactFirstName, array contactLastName, etc. Here is some part of the code:
void string_InsertionSort(contact *array, int size, char *sortField){
waitForKey();
int i,j;
contact key;
if (strcmp(sortField, "First name") == 0){
for (i = 1; i < size; i++){
key=array[i];
j = i-1;
while ( (j >= 0) && strcmp(array[j].contactFirstName, array[j].contactFirstName) > 0){
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}else if (strcmp(sortField, "Last name") == 0){
for (i = 1; i < size; i++){
key=array[i];
j= i-1;
while ( (j >= 0) && strcmp(array[j].contactLastName, array[j].contactLastName) > 0){
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
}else{
printf(" debuggggg");
}
}
I'm implementing several sorting algoritms to sort the contacts in several diferent functions.
The problem that I have is, in the string_InsertionSort function, I pass an array of contacts, the size of the array, and the sort field that I want to order. Inside the function, I compare the sorting field to the fields that I have, and if is the correct one, I make the sorting.
The problem is that I have 15 different fields, so I have to repeat all the code to the diferent fiels. There is another way? If so, can anyone, make me an exemple? (kind accessing the struct member from outside)