A C library for agnostically ordering arrays of structs by any member value. Supports multiple sorting algorithms including selection sort, quicksort, bucket sort, and radix sort.
#include "include/order.h"
struct Person {
char name[50];
int age;
};
struct Person people[3] = {
{"Alice", 30},
{"Bob", 25},
{"Carol", 35}
};
// Sort by age (ascending)
selectionSort(makeORDER(people, age), ltINT);make
./struct_orderstruct Data {
char name[20];
int priority;
};
struct Data tasks[4] = {
{"Task A", 3},
{"Task B", 1},
{"Task C", 2},
{"Task D", 1}
};
// Sort by priority (ascending)
quickSort(makeORDER(tasks, priority), ltINT);struct Student {
char name[30];
int grade;
};
struct Student students[3] = {
{"Charlie", 85},
{"Alice", 92},
{"Bob", 78}
};
// Sort by name (alphabetical)
selectionSort(makeORDER(students, name), ltSTR);typedef struct {
char title[50];
float rating;
} Movie;
Movie *movies = malloc(sizeof(Movie) * 3);
// ... populate movies ...
// Sort by rating (descending)
quickSort(makeORDER_DYN(movies, rating, 3), gtINT);selectionSort()- Simple O(n²) algorithm, good for small arraysquickSort()- Fast O(n log n) average casequickSortInd()- Indirect sorting (reorders pointers, not data)bucketSort()- Good for uniformly distributed dataradixSort()- Linear time for integers/strings
- Integers:
ltINT,gtINT,eqINT - Strings:
ltSTR,gtSTR,eqSTR - Binary strings:
ltBIN_STR,gtBIN_STR
The core concept is the OrderStruct which describes how to access and compare your data:
// For static arrays - use makeORDER macro
selectionSort(makeORDER(array, field_name), comparison_function);
// For dynamic arrays - specify size
quickSort(makeORDER_DYN(ptr, field_name, count), comparison_function);TODO: Add section on creating custom comparison functions
TODO: Add detailed examples for bucket sort and radix sort with different data types
TODO: Add algorithm complexity comparison and when to use each
MIT