c - Sorting array of structure for with two structure variable? -


i having trouble sorting following array.

how sort temp_var[] array based on temp_var[].trade_date , temp_var[].trans_amount ?

typedef struct       {     char    trans_d                  [2],                   trans_amount            [10],              trans_me                 [8],             account                 [10],              trans                  [16],             trade_date              [12],             setnt_date              [12]; } what_if;  what_if  temp_var[100];  void swap(what_if *a, what_if *b) {     tmp = *a;     *a = *b;     *b = tmp; }   void bubblesort(what_if a[], int size) {     (i=0; i<size-1; i++)     {         (j=size-1; j>i; j--)               if (strcmp(a[j].trade_date , a[j-1].trade_date) < 0 )                 swap(&a[j], &a[j-1]);     } }  int main() {     //after read structure values     bubblesort(temp_var,t_count); } 

you check 2nd sorting criteria if 1st equal. please check wether trans_amount field can compared way. code shorter, intention demonstrate how works.

int first = strcmp(a[j].trade_date , a[j-1].trade_date);  if ( first == 0 ) {      if ( strcmp(a[j].trans_amount , a[j-1].trans_amount) < 0 )          swap(&a[j], &a[j-1]);  }  else if ( first < 0 ) {     swap(&a[j], &a[j-1]);  } 

Comments