Skip to content
Related Articles

Related Articles

C program to store Student records as Structures and Sort them by Name
  • Difficulty Level : Medium
  • Last Updated : 17 Jan, 2019
GeeksforGeeks - Summer Carnival Banner

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 name.

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 = 4, Name = aaz, Age = 9 },
{Id = 5, Name = az, Age = 10 },
{Id = 2, Name = ba, Age = 10 },
{Id = 3, Name = bc, Age = 8 },
{Id = 1, Name = bd, Age = 12 } }

Approach: This problem is solved in 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 names can be sorted by the help of strcmp() method.
  • 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 Name
  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  
// struct person with 3 fields
struct Student {
    char* name;
    int id;
    char age;
};
  
// setting up rules for comparison
// to sort the students based on names
int comparator(const void* p, const void* q)
{
    return strcmp(((struct Student*)p)->name,
                  ((struct Student*)q)->name);
}
  
// Driver program
int main()
{
    int i = 0, n = 5;
  
    struct Student arr[n];
  
    // Get the students data
    arr[0].id = 1;
    arr[0].name = "bd";
    arr[0].age = 12;
  
    arr[1].id = 2;
    arr[1].name = "ba";
    arr[1].age = 10;
  
    arr[2].id = 3;
    arr[2].name = "bc";
    arr[2].age = 8;
  
    arr[3].id = 4;
    arr[3].name = "aaz";
    arr[3].age = 9;
  
    arr[4].id = 5;
    arr[4].name = "az";
    arr[4].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 Name:\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 Name:
Id = 4, Name = aaz, Age = 9 
Id = 5, Name = az, Age = 10 
Id = 2, Name = ba, Age = 10 
Id = 3, Name = bc, Age = 8 
Id = 1, Name = bd, Age = 12

Note: The structures can be sorted according to any field, based on the rules defined in the comparator function. To know more about comparator function. refer Comparator function of qsort() in C

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.




My Personal Notes arrow_drop_up
Recommended Articles
Page :