I'm trying to sort data from a .csv file based on the year (3rd field), using qsort and bubble sort to test the speed of both:


Look What The Cat Dragged In,Poison,2001,Look What The Cat Dragged In by Poison,1,0,1,0
Nothin' But A Good Time,Poison,1988,Nothin' But A Good Time by Poison,1,1,21,21
Something To Believe In,Poison,1990,Something To Believe In by Poison,1,1,1,1
Talk Dirty To Me,Poison,1978,Talk Dirty To Me by Poison,1,1,1,1
A Salty Dog,Procol Harum,1969,A Salty Dog by Procol Harum,1,1,1,1
A Whiter Shade of Pale,Procol Harum,1967,A Whiter Shade of Pale by Procol Harum,1,1,3,3
Blurry,Puddle of Mudd,2001,Blurry by Puddle of Mudd,1,1,1,1
Amie,Pure Prairie League,,Amie by Pure Prairie League,1,0,4,0
Another One Bites the Dust,Queen,1980,Another One Bites the Dust by Queen,1,1,102,102
Bicycle Race,Queen,1978,Bicycle Race by Queen,1,1,3,3
Kiss You All Over,Kiss,1978,Kiss You All Over by Kiss,1,1,5,5




Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


#define MAX 300


typedef struct {
    char *song, *artist;
    int year;
} music;




int sort_Song(const void *a, const void *b)
{
    music *p = (music *)a;
    music *q = (music *)b;
    return strcmp(p->song, q->song);
}


int sort_Artist(const void *a, const void *b)
{
    music *p = (music *)a;
    music *q = (music *)b;
    return strcmp(p->artist, q->artist);
}


int sort_year(const void *a, const void *b)
{
    music *p = (music *)a;
    music *q = (music *)b;
    return ((int)(p->year) - (int)(q->year));
}


void display(music *array, int n)
{
    int i;
    for(i = 0;i <= n; i++)
    {
        printf("Song: %s | Artist: %s | Year: %d\n", array->song, array->artist, array->year);
    }
}


int countLines(FILE *fp)
{
    char c;
    int count = 0;
    for (c = getc(fp); c != EOF; c = getc(fp))
    {
        if (c == '\n') // Increment count if this character is newline
        {
            count = count + 1;
        }
    }
    return count;
}


/*void bubbleSort_year(int array[], int n)
{
    int i,j;
    int aux;
    for (i = 0; i < (n-1); i++)
    {
        for(j = 0; j < (n-i-1); j++)
        {
            if(array[j]) > array[j+1])
            {
                aux = array[j];
                array[j] = array[j+1];
                array[j+1] = aux;
            }
        }
    }
}
*/




int main(int argc, char **argv)
{
    FILE *fin = fopen(argv[1], "r");
    if (!fin)
    {
        fprintf(stderr, "Error opening the file.\n");
        exit(1);
    }
    if(argc != 2)
    {
        fprintf(stderr, "Invalid usage.\nCorrect usage: ./a.out file.csv");
        exit(-1);
    }
    music *array = (music *)malloc(sizeof(music));
    char buf[MAX];
    while(fgets(buf, MAX, fin))
    {
        buf[strcspn(buf, "\n")] = '\0';  // strip the trailing newline
        char *fields[8];
        char *word = strtok(buf, ",");
        int i = 0;
        while (word)
        {
            fields[i++] = word;
            word = strtok(NULL,",");
        }
        char *p;
        array->song = strdup(fields[0]);
        array->artist = strdup(fields[1]);
        array->year = strtol(fields[2], &p, 10);
        printf("Song: %s | Artist: %s | Year: %d\n", array->song, array->artist, array->year);
    }
    qsort(array, countLines(fin), sizeof(music), sort_Artist);
    //printf("Song: %s | Artist: %s | Year: %lu\n", array->song, array->artist, array->year);
    display(array, countLines(fin)-1);
    free(array->artist);
    free(array->song);
    free(array);
    fclose(fin);
    return 0;
}
Right now, the data displayed is unsorted and I'm not sure what is wrong with my qsort function? Any ideas? Thank you!