Thread: Sorting dates into ascending order

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    Sorting dates into ascending order

    Hi all

    I am trying to sort dates into ascending order

    I currently have a structure to represent dates

    Code:
    struct dates{
    
    char month[10];
    int day;
    int year;
    
    
    };
    I then have an array of type struc date, which stores each date input by the user.

    My question is. How do I sort the dates into ascending order when it is input in the following format:

    Jan(date.month) 10(date.day) 01(date.year)

    I am not looking for code but rather an explanation as to how this can be achieved.

    Thank you

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Convert your struct into into an integer count of days (hours, minutes, seconds, whatever) from some starting point and sort using that value as a key. Look into [ab]using struct tm and mktime() rather than rolling your own to save you some grief.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    with the standard library function qsort you can pass it an array of your structures and a pointer to a function that does the comparison. you write that function and in it you do the comparisons in the order year,month,day. the hard part is you have a string for month so you would have to translate those strings to something that gives you the order. a lookup table or something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  2. ascending and descending order
    By ssk in forum C Programming
    Replies: 3
    Last Post: 03-25-2011, 08:03 PM
  3. 3 integers in ascending order
    By hobilla in forum C Programming
    Replies: 7
    Last Post: 02-14-2009, 01:01 PM
  4. array in ascending order
    By galmca in forum C Programming
    Replies: 4
    Last Post: 10-25-2004, 11:44 AM
  5. sorting arrays (ascending order)
    By utoots in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:57 AM