#include <stdio.h>
#include <stdlib.h>
typedef struct
{
float x;
float y;
} position_t;
#define NUM_POSITIONS 10
position_t positions[NUM_POSITIONS];
int sort_xy(void const *a, void const *b)
{
position_t *pa, *pb;
pa = (position_t *) a;
pb = (position_t *) b;
if( pa->x < pb->x) return -1;
if( pa->x > pb->x) return 1;
if( pa->y < pb->y) return -1;
if( pa->y > pb->y) return 1;
return 0;
}
int main( int argc, char **argv)
{
int i;
printf("\nInitials positions : \n");
for( i = 0 ; i < NUM_POSITIONS ; i++)
{
positions[i].x = (int)(random()) % 5;
positions[i].y = (int)(rand()) % 5;
printf(" x=%f y=%f \n", positions[i].x, positions[i].y );
}
qsort(positions, NUM_POSITIONS, sizeof(position_t), sort_xy);
printf("\n\nSorted positions : \n");
for( i = 0 ; i < NUM_POSITIONS ; i++)
{
printf(" x=%f y=%f \n", positions[i].x, positions[i].y);
}
return 0;
}