How to sort structure arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcheelol
    New Member
    • Feb 2010
    • 25

    How to sort structure arrays

    Code:
    typedef struct record1{
    int n1;
    string name1;
    char c1;
    double d1;
    } set1;
    
    int swap2(int &n1, int &n2){
    int temp;
    temp=n1;
    n1=n2;
    n2=temp;
    
    return 1; } // SWAP 2 NUM
    
    int swap2(char &n1, char &n2){
    char temp;
    temp = n1;
    n1 = n2;
    n1 = temp;
    
    return 1; } // SWAP 2 CHAR
    
    int swap2(double &n1, double &n2){
    double temp;
    temp = n1;
    n1 = n2;
    n2 = temp;
    
    return 1; } // SWAP 2 DOUBLE
    
    int swap2(char n1[20], char n2[20]){
    char temp[20];
    strcpy(temp[20],n1[20]);
    strcpy(n1[20],n2[20]);
    strcpy(n2[20],temp[20]);
    
    return 1; } // SWAP 2 STRING
    
    
    int bubble1(set1 *p){
    int i,j,temp,flag;
    for(j=0; j<90; j++){
    flag=0;
    for(i=0; i<89; i++){
    if(((p+i)->n1) > ((p+i+1)->n1)){
    swap2(((p+i)->n1), ((p+i+1)->n1));
    swap2(((p+i)->c1), ((p+i+1)->c1));
    swap2(((p+i)->d1), ((p+i+1)->d1));
    swap2(((p+i)->name1), ((p+i+1)->name1));
    flag =1;
    } // IF
    } // SORT
    if(flag==0)break;
    } // J
    
    return 0;
    }
    
    
    int main(){
    clrscr();
    int ct;
    ifstream in1("names.dat");
    set1 *s1;
    s1 = new set1[100];
    
    
    
    for(ct=0;!in1.eof();++ct){
    in1>>(s1+ct)->n1;
    if(((s1+ct)->n1) == -1)break;
    in1>>(s1+ct)->name1;
    in1>>(s1+ct)->c1;
    in1>>(s1+ct)->d1;
    } // FOR
    
    
    
    for(int i=0; i<ct; i++){
    cout << "\n";
    cout << "\t" << (s1+i)->n1;
    cout << "\t" << (s1+i)->d1;
    cout << "\t\t" << (s1+i)->c1;
    cout << "\t" << (s1+i)->name1;
    if((i%15==0)&&(i>0)){
    cout << "\n\t";
    cout << "\n\tEnter to continue...";
    getch();
    }
    } // FOR
    cout << "\n\n\t Bubble Sort in Progress...\n";
    
    for(int i=0; i<ct; i++){
    bubble1(s1);
    cout << "\n";
    cout << "\t" << (s1+i)->n1;
    cout << "\t" << (s1+i)->d1;
    cout << "\t\t" << (s1+i)->c1;
    cout << "\t" << (s1+i)->name1;
    if((i%15==0)&&(i>0)){
    cout << "\n\t";
    cout << "\n\tEnter to continue...";
    getch();
    }
    } // FOR
    
    cout << "\n\tTotal =\t" << ct;
    
    delete []s1;
    in1.close();
    cout << "\n\tComplete";
    
    
    return 0;
    } //MAIN
    i need to get this program to sort the numbers and print them out, the string function doesn't work so i don't think it sorts everything... help please!!

    i know i'm not really supposed to post the whole source code, but i think it may be a problem in the way i'm writing it. the numbers or n1 sort, but nothing else does.

    6 ACTON Y 185.000
    198 CONSTANCE Q 42.182

    thats a few things from the names.dat file so you could get familiar with it. thanks!
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    >the string function doesn't work

    Quite vague. Try to step through it in debugger. Oh wait - it doesn't even compile.

    >the numbers or n1 sort, but nothing else does.

    How do they sort if it doesn't compile?

    Comment

    • lolcheelol
      New Member
      • Feb 2010
      • 25

      #3
      i just commented out the entire block of string code. when i comment out the block it compiles fine, but it does not sort.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Just write a function that takes two pointers to your struct as arguments and inside the function compare the two structs. Return +1 if the first struct > seciond struct. Return -1 if the first struct < secnd struct. Return 0 if the two are equal.

        You can call this function inside the loop that's inside your sort.

        Or you might be able to use qsort.

        Comment

        Working...