Array of Structures: Sorting

This is a discussion on Array of Structures: Sorting within the C Programming forums, part of the General Programming Boards category; Hi! Ok, I honestly have exausted my mind over this one. I have an array of 12 structures, each one ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    24

    Array of Structures: Sorting

    Hi!

    Ok, I honestly have exausted my mind over this one.

    I have an array of 12 structures, each one has a name and several other double variables.

    I am trying to sort the entries in the structure based on the value of one of those double variables. The one with the largest value ending up at the top.

    If anyone has any suggestions for how I could go about it, im not looking for a code reply or 100% but more of a hard shove in the right direction

    Currently I have defined a structure called holder to be a temporary place to move a position in the array of structures to.
    I have also made an array of 1 of this structure so I can simply refer to it as holder[pos].name, .p etc.

    just to explain before the code, .name is a character array[20].
    .t is a double. This is the value I want to sort by.

    holders is the array of holder structure, my temporary structure.

    cars is the main structure I want to sort, cars is an array[12] of car structures.

    Code:
    int pos = 0;
    int i;
    
    for(i=0;i<12;i++)
    {
     if(cars[i].t>cars[i+1].t)
     {
    
          strcpy(holders[pos].name, cars[i].name);
          holders[pos].t = cars[i].t;
    
          strcpy(cars[i].name, cars[i+1].name);
          carss[i].t = cars[i+1].t;
    
    
          strcpy(cars[i+1].name, holders[pos].name);
          cars[i+1].t = holders[pos].t;
    
     }
    }
    }
    thanks

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Try looking up a sorting algorithm if you haven't done so already. Quicksort is generally an easy to implement sorting algorithm thats pretty fast as long as you choose the correct pivot point.

    http://www.cprogramming.com/tutorial...quicksort.html

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    more over trying to swap each data members of the struct separate which is not required
    ckeck out the following prog

    Code:
    #include<stdio.h>
    
    struct abc
    {
            char name[10];
            int i;
            float f;
    };
    
    int main()
    {
        struct abc a={"hello",2,10};
        struct abc b;
        
        b=a;
        
        printf("%s",b.name);
        getchar();
        
    }
    Code:
    outpout: -
    hello
    u can see that by using just an assigment operator to assign one struct a to struct b
    Code:
    b=a;
    s.s.harish

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    24
    Sorted! (get it? sorted...)
    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting an array of structures
    By kisiellll in forum C Programming
    Replies: 15
    Last Post: 04-06-2009, 06:25 AM
  2. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 04:17 AM
  3. trying to initialize an array of structures
    By dreamgoat in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 06:33 PM
  4. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 08:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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