qsort please help. Thanks

This is a discussion on qsort please help. Thanks within the C Programming forums, part of the General Programming Boards category; I'm facing some problem in sorting algoritm....

  1. #1
    Ann Lim
    Guest

    qsort please help. Thanks

    I'm facing some problem in sorting algoritm.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Um, What might that problem be?

  3. #3
    Ann Lim
    Guest
    sorry for the incomplete posting.

    I'm doing sorting for up to 500 name entries. These name might in Ascii char (upper case or lower case) or Unicode. So how to compare entries without case sensitive. Thanks.

  4. #4
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    use strcmp() or strncmp()
    this could check for the priority by value of ASCII.
    but you need to include <string.h>

  5. #5
    Ann Lim
    Guest
    How about if it's case sensitive. I do used "topper" to convert all entries to upper case 1st before strcmp. But it come to problems if the entries is in unicode (chinese character). So any suggestion? Thanks.

  6. #6
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    If you need the algorithum try google. heh lots of hits there
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    So basically you need string compare's for wide characters. Check out this function:

    Code:
    int wcsncmp( const wchar_t *string1, const wchar_t *string2, size_t count );
    From MSDN: ( on return type )

    < 0 string1 substring is less than string2 substring
    0 string1 substring identical to string2 substring
    > 0 string1 substring greater than string2 substring

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you need a case insensitive compare why not just make a copy string and convert the string to one case?

  9. #9
    Ann Lim
    Guest
    I used qsort to sort 500 name entries in ASCII or Unicode and here is my sort function. But the program reset when it try to run this function. But if i sort without case sensitive which mean i use strcmp directly i seem ok.

    Code:
    	
    	int status, i, j;
    	char nameStr1[PHB_MAX_LEN], nameStr2[PHB_MAX_LEN];
    	
    	memset(nameStr1, 0, PHB_MAX_LEN);
    	memset(nameStr2, 0, PHB_MAX_LEN);
    	
    	if(0x80 !=arg1->name[0])
    	{
    		for(i=0; i<PHB_MAX_LEN;i++)
    			nameStr1[i] = toupper(arg1->name[i]); 
    	}
    	if(0x80 !=arg2->name[0])
    	{
    		for(j=0; j<PHB_MAX_LEN;j++)
    			nameStr2[j] = toupper(arg2->name[j]);
    	}
    
    	if((0x80 !=arg1->name[0])&&(0x80 !=arg2->name[0]))
    		status = strcmp((char*)nameStr1, (char*)nameStr2);
    	else
    		status= memcmp( (char*)arg1->name, (char*)arg2->name, PHB_MAX_LEN);
    
    		
    	return status;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 05:18 PM
  3. trouble with qsort
    By qubit67 in forum C Programming
    Replies: 5
    Last Post: 04-29-2007, 11:23 PM
  4. Question About Using qsort
    By Zildjian in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:17 PM
  5. C++ link error with qsort
    By bvnorth in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2003, 03:22 AM

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