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

#define PEOPLE 5

struct Person {
	int age;
	const char *name;
};

typedef struct Person Person;

void myqsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *), void *tmp) {
	for (size_t i = 0; i < nmemb - 1; i++) {
		void* a = base + (size * i);
		for (size_t j = i + 1; j < nmemb; j++) {		
			void* b = base + (size * j);
			if (compar(a, b)) {
				memcpy(tmp, a, size);
				memcpy(a, b, size);
				memcpy(b, tmp, size);
			}
		}
	}
}

int comparePerson(const void * a, const void *b) {
	Person *p1 = (Person*) a;
	Person *p2 = (Person*) b;
	return p1->age > p2->age;
}

int main() {
	Person people[PEOPLE] = {
		{10, "Pedro"},
		{1, "Ana"},
		{28, "Alessandro"},
		{40, "Evandro"},
		{7, "Leroy"}
	};
	Person tmp;
	
	for (int i = 0; i < PEOPLE; i++) {
		printf("%d: %s\n", people[i].age, people[i].name);
	}
	puts("-----------------------");
	myqsort(people, PEOPLE, sizeof(Person), comparePerson, &tmp);
	
	for (int i = 0; i < PEOPLE; i++) {
		printf("%d: %s\n", people[i].age, people[i].name);
	}
	
	return 0;
}
