Algorithm to sort array of structures by date (using struct tm as memb

[hr]I am writing command-line programs in C. Given the following...

[code]#include

#define MAX 200

struct mystruct {
/* Various structure members here */
struct tm date; /* Following members of tm used: tm_mday, tm_mon, tm_year. */
} data[MAX], temp;[/code]
...I would like to sort the array of structures by date. I thought that I might work this out with qsort(), but quickly realized that it couldn't be done. I then found that I was able to accomplish this through three successive bubble sorts: by day, by month, and by year.

Of course, given what I've heard about the terrible performance of bubble sorts in real programming situations, I can't believe that my solution is really of any professional use. However, I just don't seem to be able to locate a web page suggesting anything better. So tell me, what algorithm is most commonly used to sort by date?[hr]

Comments

  • : [hr]I am writing command-line programs in C. Given the following...
    :
    : [code]#include
    :
    : #define MAX 200
    :
    : struct mystruct {
    : /* Various structure members here */
    : struct tm date; /* Following members of tm used: tm_mday, tm_mon, tm_year. */
    : } data[MAX], temp;[/code]
    : ...I would like to sort the array of structures by date. I thought that I might work this out with qsort(), but quickly realized that it couldn't be done. I then found that I was able to accomplish this through three successive bubble sorts: by day, by month, and by year.
    :
    : Of course, given what I've heard about the terrible performance of bubble sorts in real programming situations, I can't believe that my solution is really of any professional use. However, I just don't seem to be able to locate a web page suggesting anything better. So tell me, what algorithm is most commonly used to sort by date?[hr]
    :
    [blue]Why do you think it cannot be done by 'qsort()' - it has callback function as a parameter - you code it so you compare dates and return -1, 0, or 1... That's all![/blue][code]
    int comparator (const void* p1, const void* p2)
    {
    struct tm* tm1 = (struct tm*) p1;
    struct tm* tm2 = (struct tm*) p2;

    // Year is first:
    if (p1->year > p2->year) {
    return 1;
    }

    if (p1->year < p2->year) {
    return -1;
    }

    // Years are equal, check months:
    if (p1->month > p2->month) {
    return 1;
    }

    if (p1->month < p2->month) {
    return -1;
    }

    // Months are equal, check days:
    if (p1->day > p2->day) {
    return 1;
    }

    if (p1->day < p2->day) {
    return -1;
    }
    return 0; // COMPLETELY SAME!
    }
    [/code]
  • [b][red]This message was edited by stevenewilson at 2002-9-18 9:12:38[/red][/b][hr]
    [hr]Special thanks to you for taking the time to write out the complete example! I'm afraid that I might not have followed a mere explanation.

    And thanks for expanding my understanding of [blue]qsort()[/blue]. I wasn't aware that it could be made to perform more than one comparison, as I hadn't seen any such examples.

    Actually, after I posted my original message, I came to understand (I hope) more about the proper uses of the [blue]time_t[/blue] data type and the [blue]struct tm[/blue], respectively. (I have just posted a separate message regarding this.)[hr]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories