sorting array of objects...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spl

    sorting array of objects...

    I have an array of objects. I want to sort the array of a
    given numeric value that exists in each of the objects. Suppose the
    array of objects is called student and the numeric value I want to
    sort the array based on the member is called "rollno".
    ex:
    struct student{
    int rollno;
    int name;
    int status;
    };
    student obj[10]
    here, I have to sort obj of 10objects, based on rollno.

    Please give your suggestion for any fast method?
  • Pedro Graca

    #2
    Re: sorting array of objects...

    spl wrote:
    [...]
    here, I have to sort obj of 10objects, based on rollno.
    >
    Please give your suggestion for any fast method?

    Comment

    • Malcolm McLean

      #3
      Re: sorting array of objects...


      "spl" <splender.dev@g mail.comwrote in message
      news:e2b7068f-1035-4e16-8ba6-14b0a421bad1@p2 5g2000hsf.googl egroups.com...
      >I have an array of objects. I want to sort the array of a
      given numeric value that exists in each of the objects. Suppose the
      array of objects is called student and the numeric value I want to
      sort the array based on the member is called "rollno".
      ex:
      struct student{
      int rollno;
      int name;
      int status;
      };
      student obj[10]
      here, I have to sort obj of 10objects, based on rollno.
      >
      Please give your suggestion for any fast method?
      int compfunct(const void *e1, const void *e2)
      {
      const student *s1 = e1;
      const student *s2 = e2;

      return s1->rollno - s2->rollno;
      }

      in main body of code:

      qsort(obj, 10, sizeof(student) , compfunct);

      --
      Free games and programming goodies.




      Comment

      • Ben Bacarisse

        #4
        Re: sorting array of objects...

        spl <splender.dev@g mail.comwrites:
        I have an array of objects. I want to sort the array of a
        given numeric value that exists in each of the objects. Suppose the
        array of objects is called student and the numeric value I want to
        sort the array based on the member is called "rollno".
        You've been pointer to qsort and this is the right advice, but...
        ex:
        struct student{
        int rollno;
        int name;
        int status;
        };
        student obj[10]
        This looks like C++ not C. If youy are to use C++ (and I take it this
        is homework) you might be expected to do it the C++ way. To find out
        about this, post in comp.lang.c++.

        [And why is the name an int?]

        --
        Ben.

        Comment

        • pete

          #5
          Re: sorting array of objects...

          Malcolm McLean wrote:
          >
          "spl" <splender.dev@g mail.comwrote in message
          news:e2b7068f-1035-4e16-8ba6-14b0a421bad1@p2 5g2000hsf.googl egroups.com...
          I have an array of objects. I want to sort the array of a
          given numeric value that exists in each of the objects. Suppose the
          array of objects is called student and the numeric value I want to
          sort the array based on the member is called "rollno".
          ex:
          struct student{
          int rollno;
          int name;
          int status;
          };
          student obj[10]
          here, I have to sort obj of 10objects, based on rollno.

          Please give your suggestion for any fast method?
          >
          int compfunct(const void *e1, const void *e2)
          {
          const student *s1 = e1;
          const student *s2 = e2;
          >
          return s1->rollno - s2->rollno;
          }
          >
          in main body of code:
          >
          qsort(obj, 10, sizeof(student) , compfunct);
          That's undefined if s1->rollno equals INT_MAX and s2->rollno equals -1,
          and what you wrote
          is also undefined for about a zillion other scenarios.

          int compfunct(const void *e1, const void *e2)
          {
          const student *s1 = e1;
          const student *s2 = e2;

          return s2->rollno s1->rollno ? -1 : s2->rollno != s1->rollno;
          }

          --
          pete

          Comment

          Working...