Anyone knows how to sort arrays of structures?

This is a discussion on Anyone knows how to sort arrays of structures? within the C Programming forums, part of the General Programming Boards category; Hie there... Does anyone here knows how to sort arrays of structures in ascending and descending order?In the simplest way? ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    31

    Anyone knows how to sort arrays of structures?

    Hie there...

    Does anyone here knows how to sort arrays of structures in ascending and descending order?In the simplest way?

    For example my structure lookes like this:
    Code:
    struct Course{
                   char name[50];
                   char id[10];
                   char grade[3];
    };
    
    struct Record{
                   char name[30];
                   long int id;
                   char year[10];
                   char major[10];
                   float gpa;
                   struct Course course_taken[6];
    };
    If i declare struct Record student[10] ,how can i sort the members for all student[10] and display in ascending or descending ?Can i use Insertion sort?Coz that looks easier for a newbie like me, but i don't know how to use insertion sort to sort strings..

    hope to get some reply here...thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Ascending and descending based on...?
    http://cboard.cprogramming.com/showthread.php?t=78605
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    For the link
    > http://cboard.cprogramming.com/showthread.php?t=78605

    why do I get :
    "dude543, you do not have permission to access this page." ?

    The easiset way is using qsort.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    ya, i cant access that link too...

    Ascending and descending based on the type of member i want to sort.For example,if i want to sort a string ,then it is in alphabetical order...Any idea how to do that?

    do i need to have a sorted data to use qsort?

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    How can i sort strings using insertion sort?Is it possible?Coz i only understand this simply sort and im running out of time now...pls help..thanks

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by dude543
    For the link
    > http://cboard.cprogramming.com/showthread.php?t=78605

    why do I get :
    "dude543, you do not have permission to access this page." ?

    The easiset way is using qsort.
    Because that thread, which was the first of this duplicate, appears to have been deleted. This was my original reply there:

    http://cboard.cprogramming.com/showthread.php?p=535039
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95
    Thanks Dave_Sinkula. Now I can see it.
    > How can i sort strings using insertion sort?Is it possible?Coz i only understand this simply sort and im running out of time now...pls help..thanks

    You can sort it anyway you like ( like insertion sort ). when you sort strings you might use
    strcmp on two diffrent variables. On structs you should write your own compare function.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    u mean using strcmp like say strcmp(s1,s2)<0 and strcmp(s1,s2)>0 ?? will it work if i do this with insertion sort?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why are you set on insertion sort? Is it a requirement of your assignment? The function qsort is available in the standard library. If you want others you get to write them.

    When using a function such as qsort, you pass in a pointer to a comparison function. In such a function would be a call to strcmp if the array of structures will be sorted on strings, and the result of this call would be the return value of the comparison function. From an old, unrelated bit of code:
    Code:
    struct person
    {
       char name[32];
       int  num;
    };
    
    int cmp_name(const void *a, const void *b)
    {
       const struct person *x = a, *y = b;
       return strcmp(x->name, y->name);
    }
    
    /* ... */
       qsort(in, count, sizeof *in, cmp_name);
    /* ... */
    If you are doing it yourself, I would guess that you would likely have the comparison function in your sort preceding the swap.

    Why not post the whole code of your attempt thus far?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Yes, but I thought qsort() wasn't stable.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    And does that matter in this case?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by cdalten
    Yes, but I thought qsort() wasn't stable.
    Yes some implementations are not "stable". But that only means that if you sort your data multiple times with different compare-functions it doesn't preserve the order of the elements of a previous run if two elements compare equal. You never said anything about such a requirement before.
    Kurt

  13. #13
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by Dave_Sinkula
    And does that matter in this case?
    I guess it depends. If a person is going to use qsort(), they should be at least aware of some of the shortcomings in the function.

  14. #14
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    what are the shortcomings?

    Coz i'll be doing a search and sort program on a student database.I need to sort alphabetically or numerically based on the results returned the search function.btw, my datas are not pre-sorted.So, can i still use quick sort?
    Anyway, im studying on it now.....hehe...

  15. #15
    Registered User
    Join Date
    Sep 2005
    Posts
    31
    Code:
    struct Course{
    	char name[50];
    	char id[10];
    	char year[10];
    };
    
    
    struct A{
    	char name[50];
    	char id[10];
    	char grade[3];
    };
    
    
    struct Record{
    	struct Login log;
    	char name[30];
    	long int id;
    	char year[10];
    	char major[20];
    	int credit_hours;
    	float gpa;
    	char deanlist[5];
    	struct A course_taken[6];
    	struct Course not_taken[10];
    };
    This is my structure.Does anyone knows how to sort stud[a].course_taken[b].id?If i declare struct Record stud[13];

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Replies: 3
    Last Post: 05-09-2009, 11:06 AM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21