Thread: How to sort string with both alpha and number?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    How to sort string with both alpha and number?

    I store a string of both letters and numbers in the array.
    Eg. ABC123, XYZ456.
    However i want to sort by the numerical part only.
    How can i do that? I try using isdigit() but i doesn't work.

    Code:
    void selectionSort(int arr[])
    {
        int i, start, min, temp;
    
    
        for (start = 0; start < 10 -1; start++)
        {
            
            min = start;
            for (i = start+1; i < 10; i++) 
                if (isdigit (arr[i]) < isdigit (arr[min]) ) 
                    min = i;
    
    
            temp = arr[start];
            arr[start] = arr[min];
            arr[min] = temp;
        }
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Is there a known pattern to this... eg. Is it always 3 letters before the number?

    If so it's pretty easy to start at an offset into the string...
    Is digit simply says "yes" or "no" to the question, it doesn't return a comparable value, you need to to a little bit more work for that.


    Assuming 3 letters...
    Code:
    if (atoi(&arr[i][3]) < atoi(&arr[min][3]))
      { ...

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    18
    Quote Originally Posted by CommonTater View Post

    Assuming 3 letters...
    Code:
    if (atoi(&arr[i][3]) < atoi(&arr[min][3]))
      { ...
    Yup there is pattern to it. Eh it works now. but i dont really understand the way you do it. why & is required in this case? I didnt use atoi before. I thought its just convert string to integer, why does it help separate the letters and numbers?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The & gives the address of the first digit, to atoi(), which then will take all the digits, and as you stated, "turn the alpha into an integer". After doing that for two entries, the larger number can be determined, and the sorting can be done.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wonderwall View Post
    Yup there is pattern to it. Eh it works now. but i dont really understand the way you do it. why & is required in this case? I didnt use atoi before. I thought its just convert string to integer, why does it help separate the letters and numbers?
    Try this...
    Code:
    #include <stdio.h>
    
    int main (void)
      {  int i;
         char arr[15] = "This is a test";
    
         for ( i =0; i < 16; i++)
            printf("%s\n", &arr[i]);
    
         return 0;
    }
    Get it now? The offset simply tells it where to start looking at the string (You don't always have to start at the beginning)... in your case I assumed 3 letters followed by 1 or more digits... so I started looking at the string's 4th character (arr[i][3]) for atoi().

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Given a string matching the format "ABC123", you could do
    if ( strncmp( &s1[2], &s2[2], 3 ) )
    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.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Given a string matching the format "ABC123", you could do
    if ( strncmp( &s1[2], &s2[2], 3 ) )
    Ever noticed how alpha sorted numbers aren't really in numerical order?

    Plus with three letters the first digit is the 4th character at s1[3]...
    Last edited by CommonTater; 11-19-2011 at 02:16 AM.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    18
    Quote Originally Posted by CommonTater View Post
    Try this...
    Code:
    #include <stdio.h>
    
    int main (void)
      {  int i;
         char arr[15] = "This is a test";
    
         for ( i =0; i < 16; i++)
            printf("%s\n", &arr[i]);
    
         return 0;
    }
    Get it now? The offset simply tells it where to start looking at the string (You don't always have to start at the beginning)... in your case I assumed 3 letters followed by 1 or more digits... so I started looking at the string's 4th character (arr[i][3]) for atoi().

    Thanks! I finally got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. populating a string only alpha characters
    By MSF1981 in forum C Programming
    Replies: 10
    Last Post: 02-06-2009, 10:58 AM
  2. Binary sort number arranging tree
    By newtocpp in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2006, 05:19 AM
  3. C++: Converting Numeric String to Alpha String
    By JosephCardsFan in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 07:07 AM
  4. How to sort hex. number ?
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2002, 02:26 PM
  5. Help me sort the names in alpha order.
    By cazil in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2002, 02:30 PM