'Sorting multiple arrays in the struct based on the ascending order of one array in C
I am trying to sort the multiple arrays based on the ascending order of one array. Here is the example:
int a[10] = {55140, 32294, 33321, 64321, 55312}
float b[10] = {11.11, 202.22, 3213.21, 144.32, 1.32}
const char* c[10] = {+, -, -, -, +}
unsigned char* d[10] = {22DS3K, 1FGJ29, 21FD43, 98DS03, 56DK23}
Now, after arranging the array 'a' in ascending order I want to sort the other arrays. The output should look like as follows:
a[10] = {32294, 33321, 55140, 55312, 64321}
b[10] = {202.22, 3213.21, 11.11, 1.32, 144.32}
c[10] = {-, -, +, +, -}
d[10] = {1FGJ29, 21FD43, 22DS3K, 56DK23, 98DS03}
Arranging the ascending order works fine. But, I am unable to sort the other arrays. I woould like to create a function to use it in my main function. Pleased to hear some suggestions.
I have seen the below post, but did not help me.
Sorting an array based on another in C
Here is the code that I have tried:
struct Data{
int a[10];
float b[10];
const char* c[10];
unsigned char* d[10];
} data;
int data_a[10];
float data_b[10];
const char* data_c[10];
unsigned char* d[10];
void ascending(int *t; int N){
int i,j,tmp;
for(i=0;j<N;j++){
for(j=i+1;j<N;j++){
if(t[i] > t[j]){
tmp=t[i];
t[i]=t[j];
t[j]=tmp;
}}}}
int main(){
int i;
for(i=0;i<5;i++){
data.a[i] = data_a[i];
data.b[i] = data_b[i];
data.c[i] = data_c[i];
data.d[i] = data_d[i];
}
ascending(data.a, 5);
for(i=0;i<5;i++){
printf("Data is %d,%.2f,%s,%hhn\n", data.a[i],data.b[i],data.c[i],data.d[i]};
}}
May I know if I am missing something or doing something completely wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|