How to Sort an Array of Structs Based on a Member in C?
Last Updated :
19 Feb, 2024
In C, we may sometimes need to sort the array of structs based on the values of certain members. In this article, we will learn how to sort a given array of structures based on a specific member in C.
For Example,
Input:
myArrayofStructs[] = {{"Person1", 21, 160.5},
{"Person2", 20, 175.0},
{"Person3", 22, 165.5}};Output:
Sorted array based on age:
Person2, 20, 175.0
Person1, 21, 160.5
Person3, 22, 165.5
Sorting Array of Structure based on a Member in C
To sort an array of structures based on a specific member, we can use the qsort() library function. Pass a pointer to an array, the number of elements, the size of each element, and a comparison function that tells qsort how to compare two structs based on that members.
C Program to Sort an Array of Structure based on Specific Member
The below example demonstrates how we can sort an array of structures on the basis of certain members using qsort in C.
C
// C program to sort an array of structure based on specific
// member
#include <stdio.h>
#include <string.h>
// Struct representing a person
struct Person {
char name[50];
int age;
float height;
};
// Comparison function for sorting based on age
int compareByAge(const void* a, const void* b)
{
return ((struct Person*)a)->age
- ((struct Person*)b)->age;
}
int main()
{
// array of structure
struct Person people[] = { { "Person1", 21, 160.5 },
{ "Person2", 20, 175.0 },
{ "Person3", 22, 165.5 } };
int n = sizeof(people) / sizeof(people[0]);
// Sorting the array based on age using qsort()
qsort(people, n, sizeof(struct Person), compareByAge);
// Printing the sorted array
printf("Sorted array based on age:\n");
for (int i = 0; i < n; i++) {
printf("%s, %d, %.1f\n", people[i].name,
people[i].age, people[i].height);
}
return 0;
}
OutputSorted array based on age:
Person2, 20, 175.0
Person1, 21, 160.5
Person3, 22, 165.5
Time Complexity: O(n log n), time complexity of the quicksort algorithm used by qsort()
.
Auxiliary Space: O(log n)
Similar Reads
How to Sort an Array of Structs with qsort in C? Sorting C arrays of structs becomes important for many kinds of applications and one common library function that may be used for this is qsort. In this article, we will learn how to use qsort() with an array of structs in C. For Example, Input: struct Person people[] = {{"Person1", 21}, {"Person2",
2 min read
How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of
2 min read
How to Search in Array of Struct in C? In C, a struct (short for structure) is a user-defined data type that allows us to combine data items of different kinds. An array of structs is an array in which each element is of struct type. In this article, we will learn how to search for a specific element in an array of structs. Example: Inpu
2 min read
How to Use bsearch with an Array of Struct in C? The bsearch function in C is a standard library function defined in the stdlib.h header file that is used to perform binary search on array-like structure. In this article, we will learn to use bsearch with an array of struct in C. Input: struct Person people[] = { { 1, "Ram" }, { 2, "Rohan" }, { 4,
3 min read
How to Insert an Element into an Array of Structs at a Specific Position in C? In C, structs allow the users to create user-defined data types which can be used to store data of different types in a single unit. In many use cases, we might use an array of structs to store the structs in contiguous memory locations to access them sequentially. In this article, we will learn how
3 min read
How to Modify Struct Members Using a Pointer in C? In C++, we use structure to group multiple different types of variables inside a single type. These different variables are called the members of structures. In this article, we will discuss how to modify the struct member using a pointer in C. Example Input: myStruct.mem1 = 10; myStruct.mem2 = 'a';
1 min read