//program to sort records using struct from a to z
#include<stdio.h>
#include<conio.h>
struct detail
{
int roll;
char name[100];
char address[100];
}var[100],temp;
void main()
{
int i,j,n;
printf("enter total records (less or than '100')\n");
scanf("%d",&n);
printf("now enter records of students\n");
for(i=0;i<n;i++)
{
printf("enter student's roll no.\n");
scanf("%d",&var[i].roll);
printf("enter student's name\n");
scanf("%s",var[i].name);
printf("enter student's address\n");
scanf("%s",var[i].address);
}
printf("now sorting from a to z on the basis of address\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((strcmp(var[i].address,var[j].address))>0)
{
temp=var[i];
var[i]=var[j];
var[j]=temp;
}
}
}
printf("sorted data (from a to z i.e. ascending order )on the basis of address are:\n:");
for(i=0;i<n;i++)
{
printf("student's roll=%d, name=%s,student's address=%s \n",var[i].roll,var[i].name,var[i].address);
}
getch();
}
----------------------------------------------------------------------------------------------------------
//program to sort records using struct(in descending order i.e. z to a)
#include<stdio.h>
#include<conio.h>
struct detail
{
int roll;
char name[100];
char address[100];
}var[100],temp;
void main()
{
int i,j,n;
printf("enter total records (less or than '100')\n");
scanf("%d",&n);
printf("now enter records of students\n");
for(i=0;i<n;i++)
{
printf("enter student's roll no.\n");
scanf("%d",&var[i].roll);
printf("enter student's name\n");
scanf("%s",var[i].name);
printf("enter student's address\n");
scanf("%s",var[i].address);
}
printf("now sorted data (z to a) on the basis of address are:\n:);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((strcmp(var[i].address,var[j].address))<0)
{
temp=var[i];
var[i]=var[j];
var[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("student's roll=%d, name=%s,student's address=%s \n",var[i].roll,var[i].name,var[i].address);
}
#include<stdio.h>
#include<conio.h>
struct detail
{
int roll;
char name[100];
char address[100];
}var[100],temp;
void main()
{
int i,j,n;
printf("enter total records (less or than '100')\n");
scanf("%d",&n);
printf("now enter records of students\n");
for(i=0;i<n;i++)
{
printf("enter student's roll no.\n");
scanf("%d",&var[i].roll);
printf("enter student's name\n");
scanf("%s",var[i].name);
printf("enter student's address\n");
scanf("%s",var[i].address);
}
printf("now sorting from a to z on the basis of address\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((strcmp(var[i].address,var[j].address))>0)
{
temp=var[i];
var[i]=var[j];
var[j]=temp;
}
}
}
printf("sorted data (from a to z i.e. ascending order )on the basis of address are:\n:");
for(i=0;i<n;i++)
{
printf("student's roll=%d, name=%s,student's address=%s \n",var[i].roll,var[i].name,var[i].address);
}
getch();
}
----------------------------------------------------------------------------------------------------------
//program to sort records using struct(in descending order i.e. z to a)
#include<stdio.h>
#include<conio.h>
struct detail
{
int roll;
char name[100];
char address[100];
}var[100],temp;
void main()
{
int i,j,n;
printf("enter total records (less or than '100')\n");
scanf("%d",&n);
printf("now enter records of students\n");
for(i=0;i<n;i++)
{
printf("enter student's roll no.\n");
scanf("%d",&var[i].roll);
printf("enter student's name\n");
scanf("%s",var[i].name);
printf("enter student's address\n");
scanf("%s",var[i].address);
}
printf("now sorted data (z to a) on the basis of address are:\n:);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if((strcmp(var[i].address,var[j].address))<0)
{
temp=var[i];
var[i]=var[j];
var[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("student's roll=%d, name=%s,student's address=%s \n",var[i].roll,var[i].name,var[i].address);
}
getch();
}
--------------------------------------------------------------------------------
note:
Here I have done both, from 'a' to 'z' and 'z' to 'a'. You can do only one, either 'a' to 'z' or 'z' to 'a'.