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

struct kilpailijat     //The structure im using
{
    char nimi[43+1];
    float aika;
};

int compare (const void * a, const void * b)
{
    float f1 = ((struct kilpailijat *)a)->aika;
    float f2 = ((struct kilpailijat *)b)->aika;
    int result = 0;
    if (f1 < f2) 
    {
        result = -1;
    }
    else if (f1 > f2) 
    {
        result = 1;
    }
    return result;
}

void printArray(struct kilpailijat * array, int size) 
{
    for (int i = 0; i < size; i++) 
    {
        printf("%s - %0.2f\n", array[i].nimi, array[i].aika);
    }
}

int main() 
{
    int maara = 3;
    struct kilpailijat henkilo[50];
    // Populate the array with fake elements
    strcpy(henkilo[0].nimi, "Runner 1");
    henkilo[0].aika = 101.2f;
    strcpy(henkilo[1].nimi, "Runner 2");
    henkilo[1].aika = 101.1f;
    strcpy(henkilo[2].nimi, "Runner 3");
    henkilo[2].aika = 99.9f;
    
    printArray(henkilo, maara);
    printf("\n-----\n\n");
    // Sort the array
    qsort (henkilo, maara, sizeof(kilpailijat), compare);
    printArray(henkilo, maara);
    
    return 0;
}