How to sort an array of dates in C/C++?

In C programming, sorting an array of dates requires creating a structure to represent dates and implementing a custom comparison function. We use the qsort() function from the standard library to sort the array based on chronological order.

Syntax

void qsort(void *base, size_t num, size_t size, 
           int (*compare)(const void *, const void *));

Method 1: Using qsort() with Custom Comparator

This approach uses a structure to store date components and a comparison function that compares dates chronologically −

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int day, month, year;
} Date;

int compareDates(const void *a, const void *b) {
    Date *date1 = (Date *)a;
    Date *date2 = (Date *)b;
    
    if (date1->year != date2->year)
        return date1->year - date2->year;
    if (date1->month != date2->month)
        return date1->month - date2->month;
    return date1->day - date2->day;
}

void printDates(Date arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%02d/%02d/%d\n", arr[i].day, arr[i].month, arr[i].year);
    }
}

int main() {
    Date dates[] = {
        {20, 1, 2017},
        {25, 3, 2010},
        {3, 12, 1956},
        {18, 10, 1982},
        {19, 4, 2011},
        {9, 7, 2013}
    };
    
    int n = sizeof(dates) / sizeof(dates[0]);
    
    printf("Original dates:\n");
    printDates(dates, n);
    
    qsort(dates, n, sizeof(Date), compareDates);
    
    printf("\nSorted dates:\n");
    printDates(dates, n);
    
    return 0;
}
Original dates:
20/01/2017
25/03/2010
03/12/1956
18/10/1982
19/04/2011
09/07/2013

Sorted dates:
03/12/1956
18/10/1982
25/03/2010
19/04/2011
09/07/2013
20/01/2017

Method 2: Using Bubble Sort with Manual Comparison

For educational purposes, here's a manual sorting approach using bubble sort −

#include <stdio.h>

typedef struct {
    int day, month, year;
} Date;

int isEarlier(Date d1, Date d2) {
    if (d1.year < d2.year) return 1;
    if (d1.year == d2.year && d1.month < d2.month) return 1;
    if (d1.year == d2.year && d1.month == d2.month && d1.day < d2.day) return 1;
    return 0;
}

void bubbleSort(Date arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (!isEarlier(arr[j], arr[j + 1])) {
                Date temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    Date dates[] = {
        {15, 8, 2020},
        {10, 3, 2019},
        {22, 12, 2021}
    };
    
    int n = sizeof(dates) / sizeof(dates[0]);
    
    printf("Before sorting:\n");
    for (int i = 0; i < n; i++) {
        printf("%02d/%02d/%d\n", dates[i].day, dates[i].month, dates[i].year);
    }
    
    bubbleSort(dates, n);
    
    printf("\nAfter sorting:\n");
    for (int i = 0; i < n; i++) {
        printf("%02d/%02d/%d\n", dates[i].day, dates[i].month, dates[i].year);
    }
    
    return 0;
}
Before sorting:
15/08/2020
10/03/2019
22/12/2021

After sorting:
10/03/2019
15/08/2020
22/12/2021

Key Points

  • The qsort() function provides O(n log n) time complexity for efficient sorting.
  • Custom comparator functions must return negative, zero, or positive values for proper sorting.
  • Date comparison follows hierarchical order: year ? month ? day.
  • Use typedef struct to create reusable date data types.

Conclusion

Sorting dates in C is efficiently achieved using qsort() with custom comparison functions. The hierarchical comparison ensures proper chronological ordering of dates.

Updated on: 2026-03-15T12:34:16+05:30

989 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements