sorting a structure...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danglez
    New Member
    • Dec 2009
    • 2

    sorting a structure...?

    well I have a template of a structure...but when I try to add something to sort the list (an attempt at a bubble sort[our instructor gave us a template about a bubble sort but I'm having troubles following it]), I'm running into some problems....any input would be appreciated, thanks

    this is all inside of a struct a[...]

    (program runs and user have 2 options of either entering a new item, and sorting that listed items...the entering a new item part I'm done but the sorting I'm having some trouble)

    the errors that come up
    "invalid operators to binary>" (line with "if statement")
    "incompatib le types in assignment" (line inside of if statement : t=...)
    "incompatib le types in assignment" (same line as above)


    void sort(void)

    {
    int j, t;

    for(j=1; j && n; j++)
    {
    if(a[j-1] > a[j]);
    {
    t = a[j-1]; a[j-1] = a[j]; a[j]=t;
    }
    }
    printf("\nSorte d List:\n");
    for (n=0; n < j; n++) // print sorted list
    printf("Name: %s\n", j+1, a[j]);
    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    How do you know that struct a a1 is less than struct a a2?

    Comment

    • danglez
      New Member
      • Dec 2009
      • 2

      #3
      sorry I'm not too sure what you mean? Do you mean in the if statement?

      Comment

      • RRick
        Recognized Expert Contributor
        • Feb 2007
        • 463

        #4
        Your problem is with various comparisons and assignments for the variable called a. They will only work is a[j] is a number, which means the variable a must be an array of ints or some other type of number.

        If a is an array of objects or structs, you have to define how the operators work.

        Comment

        Working...