i've been trying to sort 2D array with qsort like:
I want to sort it with qsort increasing or decreasing by x value, and if x values are same, i want it to compare the y values. it should be printing like this:
1, 2 - 2, 3 - 2, 4
I tried so hard, please help me.
#include <stdio.h> #include <stdlib.h> struct testt{ int start; int len; }; int comparePlease(const void* p, const void* q) { struct testt* x = (struct testt*)p; struct testt* y = (struct testt*)q; return *x - *y; } int main() { struct testt test[5] = { {40, 20}, {50, 30}, {30, 100}, {50, 35}, {25, 15} }; qsort(test, sizeof(test) / sizeof(test[0]), sizeof(int), comparePlease); }
qsort? Please provide a minimal reproducible example.sizeof(int)->sizeof(struct test)sizeof(test[0])