# C program to store Student records as Structures and Sort them by Age or ID

Given student’s records with each record containing id, name and age of a student. Write a C program to read these records and display them in sorted order by age or id.

### Sorting by Age

Examples:

```Input: Student Records = {
{Id = 1, Name = bd, Age = 12 },
{Id = 2, Name = ba, Age = 10 },
{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 5, Name = az, Age = 10 } }

Output:
{{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 2, Name = ba, Age = 10 },
{Id = 5, Name = az, Age = 10 },
{Id = 1, Name = bd, Age = 12 } }
```

Approach: This problem is solved in the following steps:

• Create a structure with fields id, name and age.
• Read the students records in the structure
• Define a comparator by setting up rules for comparison. Here age can be sorted with the help of difference of the age of 2 students. (Student1 -> age – Student2 -> age)
• Now sort the structure based on the defined comparator with the help of qsort() method.
• Print the sorted students’ records.

Program:

 `// C program to read Student records ` `// like id, name and age, ` `// and display them in sorted order by Age ` ` `  `#include ` `#include ` `#include ` ` `  `// struct person with 3 fields ` `struct` `Student { ` `    ``char``* name; ` `    ``int` `id; ` `    ``char` `age; ` `}; ` ` `  `// setting up rules for comparison ` `// to sort the students based on age ` `int` `comparator(``const` `void``* p, ``const` `void``* q) ` `{ ` `    ``return` `(((``struct` `Student*)p)->age - ((``struct` `Student*)q)->age); ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `i = 0, n = 5; ` ` `  `    ``struct` `Student arr[n]; ` ` `  `    ``// Get the students data ` `    ``arr.id = 1; ` `    ``arr.name = ``"bd"``; ` `    ``arr.age = 12; ` ` `  `    ``arr.id = 2; ` `    ``arr.name = ``"ba"``; ` `    ``arr.age = 10; ` ` `  `    ``arr.id = 3; ` `    ``arr.name = ``"bc"``; ` `    ``arr.age = 8; ` ` `  `    ``arr.id = 4; ` `    ``arr.name = ``"aaz"``; ` `    ``arr.age = 9; ` ` `  `    ``arr.id = 5; ` `    ``arr.name = ``"az"``; ` `    ``arr.age = 10; ` ` `  `    ``// Print the Unsorted Structure ` `    ``printf``(``"Unsorted Student Records:\n"``); ` `    ``for` `(i = 0; i < n; i++) { ` `        ``printf``(``"Id = %d, Name = %s, Age = %d \n"``, ` `               ``arr[i].id, arr[i].name, arr[i].age); ` `    ``} ` `    ``// Sort the structure ` `    ``// based on the specified comparator ` `    ``qsort``(arr, n, ``sizeof``(``struct` `Student), comparator); ` ` `  `    ``// Print the Sorted Structure ` `    ``printf``(``"\n\nStudent Records sorted by Age:\n"``); ` `    ``for` `(i = 0; i < n; i++) { ` `        ``printf``(``"Id = %d, Name = %s, Age = %d \n"``, ` `               ``arr[i].id, arr[i].name, arr[i].age); ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output:

```Unsorted Student Records:
Id = 1, Name = bd, Age = 12
Id = 2, Name = ba, Age = 10
Id = 3, Name = bc, Age = 8
Id = 4, Name = aaz, Age = 9
Id = 5, Name = az, Age = 10

Student Records sorted by Age:
Id = 3, Name = bc, Age = 8
Id = 4, Name = aaz, Age = 9
Id = 2, Name = ba, Age = 10
Id = 5, Name = az, Age = 10
Id = 1, Name = bd, Age = 12
```

### Sorting by Id

Examples:

```Input: Student Records = {
{Id = 1, Name = bd, Age = 12 },
{Id = 2, Name = ba, Age = 10 },
{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 5, Name = az, Age = 10 } }

Output:
{{Id = 1, Name = bd, Age = 12 },
{Id = 2, Name = ba, Age = 10 },
{Id = 3, Name = bc, Age = 8 },
{Id = 4, Name = aaz, Age = 9 },
{Id = 5, Name = az, Age = 10 } }
```

Approach: This problem is solved in the following steps:

• Create a structure with fields id, name and age.
• Read the students records in the structure
• Define a comparator by setting up rules for comparison. Here id can be sorted with the help of difference of the id of 2 students. (Student1 -> id – Student2 -> id)
• Now sort the structure based on the defined comparator with the help of qsort() method.
• Print the sorted students’ records.

Program:

 `// C program to read Student records ` `// like id, name and age, ` `// and display them in sorted order by ID ` ` `  `#include ` `#include ` `#include ` ` `  `// struct person with 3 fields ` `struct` `Student { ` `    ``char``* name; ` `    ``int` `id; ` `    ``char` `age; ` `}; ` ` `  `// setting up rules for comparison ` `// to sort the students based on ID ` `int` `comparator(``const` `void``* p, ``const` `void``* q) ` `{ ` `    ``return` `(((``struct` `Student*)p)->id - ((``struct` `Student*)q)->id); ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `i = 0, n = 5; ` ` `  `    ``struct` `Student arr[n]; ` ` `  `    ``// Get the students data ` `    ``arr.id = 1; ` `    ``arr.name = ``"bd"``; ` `    ``arr.age = 12; ` ` `  `    ``arr.id = 2; ` `    ``arr.name = ``"ba"``; ` `    ``arr.age = 10; ` ` `  `    ``arr.id = 3; ` `    ``arr.name = ``"bc"``; ` `    ``arr.age = 8; ` ` `  `    ``arr.id = 4; ` `    ``arr.name = ``"aaz"``; ` `    ``arr.age = 9; ` ` `  `    ``arr.id = 5; ` `    ``arr.name = ``"az"``; ` `    ``arr.age = 10; ` ` `  `    ``// Print the Unsorted Structure ` `    ``printf``(``"Unsorted Student Records:\n"``); ` `    ``for` `(i = 0; i < n; i++) { ` `        ``printf``(``"Id = %d, Name = %s, Age = %d \n"``, ` `               ``arr[i].id, arr[i].name, arr[i].age); ` `    ``} ` `    ``// Sort the structure ` `    ``// based on the specified comparator ` `    ``qsort``(arr, n, ``sizeof``(``struct` `Student), comparator); ` ` `  `    ``// Print the Sorted Structure ` `    ``printf``(``"\n\nStudent Records sorted by ID:\n"``); ` `    ``for` `(i = 0; i < n; i++) { ` `        ``printf``(``"Id = %d, Name = %s, Age = %d \n"``, ` `               ``arr[i].id, arr[i].name, arr[i].age); ` `    ``} ` ` `  `    ``return` `0; ` `} `

Output:

```Unsorted Student Records:
Id = 1, Name = bd, Age = 12
Id = 2, Name = ba, Age = 10
Id = 3, Name = bc, Age = 8
Id = 4, Name = aaz, Age = 9
Id = 5, Name = az, Age = 10

Student Records sorted by ID:
Id = 1, Name = bd, Age = 12
Id = 2, Name = ba, Age = 10
Id = 3, Name = bc, Age = 8
Id = 4, Name = aaz, Age = 9
Id = 5, Name = az, Age = 10
```

My Personal Notes arrow_drop_up

## Recommended Posts:

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.