My Program contains 4 arrays and I want to sort the array in such a way that if one array is sorted the other array should follow it
Example:

unsorted array
name    code salary date
John    52    6500    15
Suzzy   10    1500    20
Mike    20    1451    16


Sorted array(according to date)
name   code   salary   date
Suzzy   10    1500      20
Mike    20    1451      16
John    10    6500      15

How can I do this type of sorting....?

#include<stdio.h>
#include<conio.h>
struct employ
{
char name[20];
int code;
int salary;
int date;
};
void main()
{
int i;
struct employ e[5];
printf("Enter Details\n");
printf("As per the format given below\ncode,name,salary,date\n");
for(i=0;i<5;i++)
{
printf("\n");
printf("No.%d\n",i+1);
scanf("%d",&e[i].code);
scanf("%s",&e[i].name);
scanf("%d",&e[i].salary);
scanf("%d",&e[i].date);
}
printf(" -----------------------------------------------\n");
printf("| code|        Name         |  Salary  |  Date  |\n");
printf(" -----------------------------------------------\n");
for(i=0;i<5;i++)
{
printf("| %-4d|",e[i].code);
printf(" %-20.20s|",e[i].name);
printf("Rs.%6d |",e[i].salary);
printf("%-8d|\n",e[i].date);
}
printf(" -----------------------------------------------\n");
getch();
}

It looks like your program actually has just one array that needs sorting, struct employ e[5];

This code would sort e[0] and e[1], for example.

if (e[0].date < e[1].date) // the sort criterion, currently sorting e[0] and e[1]
{
  // code here to swap e[0] and e[1];
}

But If I sort date with regular sorting code it just sort the date array and everything else remain the same.As I want to sort date and the corresponding array should also follow it...

You don't have a date array. You have an array of employ objects. You look at the date INSIDE the employ objects to see what order they should be in, and you move the WHOLE employ object when you do the sorting.

Sort for date

#include<stdio.h>
#include<conio.h>
struct employ
{
char name[20];
int code;
int salary;
int date;
};
void main()
{
int i,n,j,temp;
struct employ e[10];
printf("Enter number of employess\n");
scanf("%d",&n);
printf("Enter Details\n");
printf("As per the format given below\ncode,name,salary,date\n");
for(i=0;i<n;i++)
{
printf("\n");
printf("No.%d\n",i+1);
scanf("%d",&e[i].code);
scanf("%s",&e[i].name);
scanf("%d",&e[i].salary);
scanf("%d",&e[i].date);
}
printf(" -----------------------------------------------\n");
printf("| code|        Name         |  Salary  |  Date  |\n");
printf(" -----------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("| %-4d|",e[i].code);
printf(" %-20.20s|",e[i].name);
printf("Rs.%6d |",e[i].salary);
printf("%-8d|\n",e[i].date);
}
printf(" -----------------------------------------------\n");

for (i=0;i<=n-1;i++)
{
     for(j=0;j<=n-2;j++)
     {
          if(e[j].salary<e[j+1].salary)
          {
                temp=e[j];
                e[i]=e[j+1];
                e[j+1]=temp;
          }
     }
}

printf(" \n\n\n Sort by salary\n");
printf("-----------------------------------------------\n");
printf("| code|        Name         |  Salary  |  Date  |\n");
printf(" -----------------------------------------------\n");
for(i=0;i<5;i++)
{
printf("| %-4d|",e[i].code);
printf(" %-20.20s|",e[i].name);
printf("Rs.%6d |",e[i].salary);
printf("%-8d|\n",e[i].date);
}
printf(" -----------------------------------------------\n");
getch();
}

Which one is correct the one above or this one

#include<stdio.h>
#include<conio.h>
struct employ
{
char name[20];
int code;
int salary;
int date;
};
void main()
{
int i,n,j,temp;
struct employ e[10],temp[10];
printf("Enter number of employess\n");
scanf("%d",&n);
printf("Enter Details\n");
printf("As per the format given below\ncode,name,salary,date\n");
for(i=0;i<n;i++)
{
printf("\n");
printf("No.%d\n",i+1);
scanf("%d",&e[i].code);
scanf("%s",&e[i].name);
scanf("%d",&e[i].salary);
scanf("%d",&e[i].date);
}
printf(" -----------------------------------------------\n");
printf("| code|        Name         |  Salary  |  Date  |\n");
printf(" -----------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("| %-4d|",e[i].code);
printf(" %-20.20s|",e[i].name);
printf("Rs.%6d |",e[i].salary);
printf("%-8d|\n",e[i].date);
}
printf(" -----------------------------------------------\n");
for (i=0;i<=n-1;i++)
{
     for(j=0;j<=n-2;j++)
     {
          if(e[j].salary<e[j+1].salary)
          {
                temp[i]=e[j];
                e[i]=e[j+1];
                e[j+1]=temp[i];
          }
     }
}
printf(" \n\n\n Sort by salary\n");
printf("-----------------------------------------------\n");
printf("| code|        Name         |  Salary  |  Date  |\n");
printf(" -----------------------------------------------\n");
for(i=0;i<5;i++)
{
printf("| %-4d|",e[i].code);
printf(" %-20.20s|",e[i].name);
printf("Rs.%6d |",e[i].salary);
printf("%-8d|\n",e[i].date);
}
printf(" -----------------------------------------------\n");
getch();
}

You have more than one object named temp in the second. This is bad.

The first has only one object named temp, but it is an int, and you're trying to use it as if it were something else. This is bad.

Neither of them compile, so they're both wrong.

So how should I proceed now..?should i remove temp from int declaration.

#include<stdio.h>
#include<conio.h>
struct employ
{
char name[20];
int code;
int salary;
int date;
};
void main()
{
int i,n,j;
struct employ e[10],temp[10];
printf("Enter number of employess\n");
scanf("%d",&n);
printf("Enter Details\n");
printf("As per the format given below\ncode,name,salary,date\n");
for(i=0;i<n;i++)
{
printf("\n");
printf("No.%d\n",i+1);
scanf("%d",&e[i].code);
scanf("%s",&e[i].name);
scanf("%d",&e[i].salary);
scanf("%d",&e[i].date);
}
printf(" -----------------------------------------------\n");
printf("| code|        Name         |  Salary  |  Date  |\n");
printf(" -----------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("| %-4d|",e[i].code);
printf(" %-20.20s|",e[i].name);
printf("Rs.%6d |",e[i].salary);
printf("%-8d|\n",e[i].date);
}
printf(" -----------------------------------------------\n");
for (i=0;i<=n-1;i++)
{
     for(j=0;j<=n-2;j++)
     {
          if(e[j].salary<e[j+1].salary)
          {
                temp[i]=e[j];
                e[i]=e[j+1];
                e[j+1]=temp[i];
          }
     }
}
printf(" \n\n\n Sort by salary\n");
printf("-----------------------------------------------\n");
printf("| code|        Name         |  Salary  |  Date  |\n");
printf(" -----------------------------------------------\n");
for(i=0;i<5;i++)
{
printf("| %-4d|",e[i].code);
printf(" %-20.20s|",e[i].name);
printf("Rs.%6d |",e[i].salary);
printf("%-8d|\n",e[i].date);
}
printf(" -----------------------------------------------\n");
getch();
}

Line 23, remove the & from that line.

rename struct temp tempSt, and make changes so it's clear which is which.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.