Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Generic List and n-criteria sorting

Generic List and n-criteria sorting

Scheduled Pinned Locked Moved C / C++ / MFC
helpalgorithmstutorialannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mariano Lopez Gappa
    wrote on last edited by
    #1

    I need to add nodes to a list regardless of its structure...I don't know how to do that since when the pointer is declared, it points to a certain data type, which needs to be generic... Moreover I need a way to sort that given list on many criteria, where maximum n is the number of fields in the structure, and those may be numeric or alphanumeric. (what I did before was if (crit1a = crit1b)&&(crit2a>crit2b) {switch...}, a more elaborate version of that, but my problem is mainly the generic issue) ............I'm so helpless..... :) Thx in advance!!!

    D C 2 Replies Last reply
    0
    • M Mariano Lopez Gappa

      I need to add nodes to a list regardless of its structure...I don't know how to do that since when the pointer is declared, it points to a certain data type, which needs to be generic... Moreover I need a way to sort that given list on many criteria, where maximum n is the number of fields in the structure, and those may be numeric or alphanumeric. (what I did before was if (crit1a = crit1b)&&(crit2a>crit2b) {switch...}, a more elaborate version of that, but my problem is mainly the generic issue) ............I'm so helpless..... :) Thx in advance!!!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Mariano Lopez-Gappa wrote:

      since when the pointer is declared, it points to a certain data type, which needs to be generic...

      Just make it a void pointer. When it comes time to dereference those items in the list, however, you'll need to know what type each points to. Consider:

      void main( void )
      {
      int Int = 5;
      double Double = 12.34;
      void *pVoid;

      pVoid = ∬
      printf("%d\\n", \*((int \*) pVoid));
      
      pVoid = &Double;
      printf("%f\\n", \*((double \*) pVoid));
      

      }


      "Take only what you need and leave the land as you found it." - Native American Proverb

      M 1 Reply Last reply
      0
      • M Mariano Lopez Gappa

        I need to add nodes to a list regardless of its structure...I don't know how to do that since when the pointer is declared, it points to a certain data type, which needs to be generic... Moreover I need a way to sort that given list on many criteria, where maximum n is the number of fields in the structure, and those may be numeric or alphanumeric. (what I did before was if (crit1a = crit1b)&&(crit2a>crit2b) {switch...}, a more elaborate version of that, but my problem is mainly the generic issue) ............I'm so helpless..... :) Thx in advance!!!

        C Offline
        C Offline
        carks
        wrote on last edited by
        #3

        Hi, I think that each "certain data type" must have a common base class, which could be as simple as class CBase { ... Constructor/Destructor... UINT m_type; // The class type. UINT GetType () { return m_type; }; }; Suppose you had various other classes CA, CB, CC, CD derived from CBase Then the comparison routine for the sort could switch in the following way int Compare( const void *e1, const void *e2 ) { // handle element e1 switch ( ((CBase *)e1)->GetType() ) { case TYPEA: CA *A = (CA *)e1; --- set some criteria based on CA info --- break; case ... etc. // handle element e2 switch ( ((CBase *)e2)->GetType() ) { case TYPEA: CA *A = (CA *)e1; --- set some criteria based on CA info --- break; case ... etc. // Now make the comparison based on information generated in the // last 2 steps. --- comparison code --- } Id be interested to hear if anybody can think of a more general/elegant way to do it! Cheers, Dave

        1 Reply Last reply
        0
        • D David Crow

          Mariano Lopez-Gappa wrote:

          since when the pointer is declared, it points to a certain data type, which needs to be generic...

          Just make it a void pointer. When it comes time to dereference those items in the list, however, you'll need to know what type each points to. Consider:

          void main( void )
          {
          int Int = 5;
          double Double = 12.34;
          void *pVoid;

          pVoid = ∬
          printf("%d\\n", \*((int \*) pVoid));
          
          pVoid = &Double;
          printf("%f\\n", \*((double \*) pVoid));
          

          }


          "Take only what you need and leave the land as you found it." - Native American Proverb

          M Offline
          M Offline
          Mariano Lopez Gappa
          wrote on last edited by
          #4

          thx for the help. it's been really useful. i'm a step closer to the solution but theres still an issue: the actual matter about the generic list is that, in this program, the user gets to decide what "fields" of a database get on the list. so for each user, or even for the same user changing the configuration, I get a different kind of list. so I gotta work on some kind of generic way to use those list, otherwise the number of field combinations are virtually infinite! another thing...I haven't been able to figure out a way to apply the n-criteria sorting, the numeric-alphanumeric thing is easy, but first of all I don't know WHICH or HOW MANY fields I have, and also it gets confusing how to apply the sorting in a generic way... this problem looks pretty complex...hope someone gets it right! thx in advance!! :D

          J 1 Reply Last reply
          0
          • M Mariano Lopez Gappa

            thx for the help. it's been really useful. i'm a step closer to the solution but theres still an issue: the actual matter about the generic list is that, in this program, the user gets to decide what "fields" of a database get on the list. so for each user, or even for the same user changing the configuration, I get a different kind of list. so I gotta work on some kind of generic way to use those list, otherwise the number of field combinations are virtually infinite! another thing...I haven't been able to figure out a way to apply the n-criteria sorting, the numeric-alphanumeric thing is easy, but first of all I don't know WHICH or HOW MANY fields I have, and also it gets confusing how to apply the sorting in a generic way... this problem looks pretty complex...hope someone gets it right! thx in advance!! :D

            J Offline
            J Offline
            John M Drescher
            wrote on last edited by
            #5

            Mariano Lopez-Gappa wrote:

            the user gets to decide what "fields" of a database get on the list.

            This is probably a silly question but why not use the database to do the sorting for you? I do this all the time. I create a query based on what fields the user wants. Too slow or possibly you are making your own database?? John

            M 1 Reply Last reply
            0
            • J John M Drescher

              Mariano Lopez-Gappa wrote:

              the user gets to decide what "fields" of a database get on the list.

              This is probably a silly question but why not use the database to do the sorting for you? I do this all the time. I create a query based on what fields the user wants. Too slow or possibly you are making your own database?? John

              M Offline
              M Offline
              Mariano Lopez Gappa
              wrote on last edited by
              #6

              I'm not going to use MFC, nor any database as I need my program to work on a 486 dx2. Also the purpose of the list is to display multisorted information on a grid. Thx anyway! :D keep talking ppl plz!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups