HELP! sorting string alphabetically
May 9, 2012 at 7:24pm UTC
I am trying to sort the strings in array 'city' in alphabetical order. I am also re-arranging the content in array 'zip' to match the sorted city array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
void display(char city[][15],char zip[][15],int &amount){
char *temp, *temp2;
char *arrayCity[6],*arrayZip[6];
for (int i=0; i<amount; i++){
arrayCity[i]=city[i];
}
for (int i=0; i<amount; i++){
arrayZip[i]=zip[i];
}
for (int j=0; j<amount; j++){
if (strcmp(arrayCity[j],arrayCity[j+1])>0){
temp=arrayCity[j];
temp2=arrayZip[j];
arrayCity[j]=arrayCity[j+1];
arrayZip[j]=arrayZip[j+1];
arrayCity[j+1]=temp;
arrayZip[j+1]=temp2;
}
}
for (int m=0; m<amount; m++){
cout<<arrayCity[m]<<", " <<arrayZip[m]<<endl;
}
}
May 9, 2012 at 8:00pm UTC
Topic archived. No new replies allowed.