How to Sort an Array of Structs with qsort in C? Last Updated : 19 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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", 22}, {"Person3", 20}}; Output: Sorted Array (by age): Person3 20 Person1 21 Person2 22 Sorting Array of Structure in CTo sort an array of structures using qsort(), we will have to provide the custom comparator function that compares the structures based on their member values. ApproachFirst, define the structure that will be stored in an array.Then, write a comparison function to compare two elements of the array that returns an integer less than, equal to, or greater than zero depending on whether the first argument is less than, equal to, or greater than the second.Finally, use the qsort() and pass the pointer to the array, the number of elements in the array, the size of each element, and a comparison function to sort the array of structs.C Program to Sort Array of Structure Using qsortThe below example demonstrates how we can sort an array of structs using qsort in C. Here, we are sorting the array based on the age member. C // C Program to sort an Array of Structs using qsort #include <stdio.h> #include <stdlib.h> #include <string.h> // Struct definition struct Person { char name[30]; int age; }; // Comparison function for qsort int comparePeople(const void* a, const void* b) { return ((struct Person*)a)->age - ((struct Person*)b)->age; } int main() { // Array of structs struct Person people[] = { { "Person1", 21 }, { "Person2", 22 }, { "Person3", 20 } }; int numPeople = sizeof(people) / sizeof(people[0]); // Display original array printf("Original Array:\n"); for (int i = 0; i < numPeople; i++) { printf("%s\t%d\n", people[i].name, people[i].age); } // Sorting using qsort qsort(people, numPeople, sizeof(struct Person), comparePeople); // Display sorted array printf("\nSorted Array (by age):\n"); for (int i = 0; i < numPeople; i++) { printf("%s\t%d\n", people[i].name, people[i].age); } return 0; } OutputOriginal Array: Person1 21 Person2 22 Person3 20 Sorted Array (by age): Person3 20 Person1 21 Person2 22 Time Complexity: O(n log n), time complexity of the quicksort algorithm used by qsort().Auxiliary Space: O(log n) Comment More infoAdvertise with us Next Article How to Pass Array of Structure to a Function in C? 21211a4uyj Follow Improve Article Tags : C Programs C Language C-Arrays C-Structure & Union C Examples +1 More Similar Reads How to Sort an Array of Structs Based on a Member in C? 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 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 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 Pass Array of Structure to a Function in C? An array of structures in C is a data structure that allows us to store multiple records of different data types in a contiguous memory location where each element of the array is a structure. In this article, we will learn how to pass an array of structures from one function to another in C. Passin 2 min read How to Access Array of Structure in C? In C, we can create an array whose elements are of struct type. In this article, we will learn how to access an array of structures in C. For Example, Input:myArrayOfStructs = {{'a', 10}, {'b', 20}, {'A', 9}}Output:Integer Member at index 1: 20Accessing Array of Structure Members in CWe can access t 2 min read How to Add an Element to an Array of Structs in C? In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra 3 min read Like