qsort not sorting

This is a discussion on qsort not sorting within the C Programming forums, part of the General Programming Boards category; I have been going through the qsort questions/responses and still cannot figure out what Im doing wrong. The following is ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    qsort not sorting

    I have been going through the qsort questions/responses and still cannot figure out what Im doing wrong. The following is what I have:

    Code:
    int compareface(const void* c1, const void *c2)
    {
        card cd1,cd2;
    
        cd1=*((card*) c1);           
        cd2=*((card*) c2);           
    
        cd1= (cd1&0x3c)>>2;          
        cd2= (cd2&0x3c)>>2;          
    
        if(cd1>cd2)
          return 1;
        if(cd1==cd2)                 
          return 0;
    
        return -1;
    }
    Code:
      typedef unsigned char card;
    
    // within main
    
      card hands[5][5];
    
      for(hand=0;hand<5;hand++)      
      {
          for (i=0; i<5; i++)
             qsort (hands[hand], 5, sizeof(hands[0]), compareface);
      }
    When I display the results, it's not in order...can anyone help?
    simple is always an understatement.....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > qsort (hands[hand], 5, sizeof(hands[0]), compareface);
    You want the size of each element of the array, not the size of the whole array

    qsort (hands[hand], 5, sizeof(hands[hand][0]), compareface);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by sweets
    I
    <...>
    Code:
      typedef unsigned char card;
    
    // within main
    
      card hands[5][5];
    
      for(hand=0;hand<5;hand++)      
      {
          for (i=0; i<5; i++)
             qsort (hands[hand], 5, sizeof(hands[0]), compareface);
      }
    When I display the results, it's not in order...can anyone help?
    Yes.
    • The 'for (i=0; i<5; i++)' thing is useless.
    • The size of the element is not 'sizeof hands[0]', but 'sizeof hands[0][0]'
    Emmanuel Delahaye

    "C is a sharp tool"

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    Being a beginner, I would have never picked it up.

    Thanx guys
    simple is always an understatement.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 11:09 AM
  3. Can I use STL qsort() for sorting structure?
    By franziss in forum C++ Programming
    Replies: 14
    Last Post: 09-20-2005, 05:34 AM
  4. sorting a linked list with qsort()
    By caduardo21 in forum C Programming
    Replies: 12
    Last Post: 04-19-2005, 08:08 PM
  5. sorting union of structures with q sort
    By colw in forum C Programming
    Replies: 6
    Last Post: 04-09-2002, 09:51 AM

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