Thread: Sort alphabetically and by number

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Sort alphabetically and by number

    Hello. I faced a problem trying to sort list alphabetically and then by number.
    e.g.
    surname name A 8
    surname name B 1
    surname name B 2
    Here is my code what i tried so far
    Code:
    void insert( ListPtr *sPtr, float value, char surname[50], char name[50], char alpha )
    {
    ListPtr newPtr, previousPtr, currentPtr;
    newPtr = (ListPtr) malloc( sizeof( List ) );
    if ( newPtr != NULL ) { 
    strncpy (newPtr->surname, surname, 50);
    strncpy (newPtr->name, name, 50);
    newPtr->alpha = alpha;
    newPtr->value = value;
    newPtr->nextPtr = NULL;
    previousPtr = NULL;
    currentPtr = *sPtr;
    while ( currentPtr != NULL && value > currentPtr->value && (int)currentPtr->alpha >=(int)alpha ) {
    previousPtr = currentPtr; 
    currentPtr = currentPtr->nextPtr; 
    }
    if ( previousPtr == NULL ) {
    newPtr->nextPtr = *sPtr;
    *sPtr = newPtr;
    }
    else {
    previousPtr->nextPtr = newPtr;
    newPtr->nextPtr = currentPtr;
    }
    }
    else
    printf( " Error.\n", );
    }
    I am getting my list sorted only by "value" but not alphabetically.

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    int result = strcmp(currentPtr->surname, newPtr->surname);
    PS.
    Code:
    if (you_use_indents)
    {
        the_code_looks_much_better = TRUE;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Code:
    while ( currentPtr != NULL && value > currentPtr->value && strcmp( currentPtr->alpha, alpha ) >= 0)

    Code:
    invalid conversion from `char' to `const char*' 
    initializing argument 1 of `int strcmp(const char*, const char*)' 
    invalid conversion from `char' to `const char*' 
    initializing argument 2 of `int strcmp(const char*, const char*)'
    :/ Don't know what is wrong.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Damn you people that typedef pointer types and then expect someone to magically figure out what the hell is wrong without the complete code.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Quote Originally Posted by krakatao View Post
    :/ Don't know what is wrong.
    The compiler is telling you exactly what is wrong. strcmp() needs pointer to string, not single char value. I gave you the example how to use this function in your code, why have you screwed it up?

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    When i am using
    Code:
    int letter=(int)alpha;
    Code:
    while ( currentPtr != NULL  && (int)currentPtr->alpha<=letter)
    program sorts list by letters ABC...
    when i am using
    Code:
    while ( currentPtr != NULL   && value > currentPtr->value )
    program sorts list by "value"
    but when i am using them together
    Code:
    while ( currentPtr != NULL  && (int)currentPtr->alpha<=letter && value > currentPtr->value)
    program does not sort the list :/.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should use a structure for this.

    Sort by sirname.
    Sort each group of identical sirnames by name.
    Sort each group of idential sirnames+name by alpha.
    Sort each group of identical sirnames+name+alpha by number.

    So all you really need is a way to identify groups, and then to give the range of that group so that you can sort that range by a field you specify.
    Code:
    void sortby( struct namethingy allnames[], int size, int field );
    allnames = An array of structures of all of your data to be sorted.
    size = How many elements you are sorting.
    field = What element in your structure you are sorting by.

    Now lets say we have 10 things. We are sorting indexes 5,6,7,8:
    Code:
    sortby( mynames + lowrange, highrange, fieldnumber );
    Something like that. Naturally you want a loop that just runs through 'mynames', and identifies if the current name contents match the previous one, and as long as they do, you increment highrange. Once they don't, you call the function as shown above, with whatever field you happen to be on for sorting.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf( " Error.\n", );
    How does this even compile?

    I'm assuming from the complete lack of indentation that you're just copy/pasting from some other forum.
    Well there's a surprise -> Sort alphabetically and by number - Dev Shed

    Notes on indentation -> SourceForge.net: Indentation - cpwiki

    Notes on NOT spamming forums -> How To Ask Questions The Smart Way
    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.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    He does it every time Salem, and like the honey badger, he just doesn't give a .........

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by krakatao View Post
    When i am using
    Code:
    while ( currentPtr != NULL  && (int)currentPtr->alpha<=letter && value > currentPtr->value)
    program does not sort the list :/.
    Correct. That kind of comparison would not place the items in any useful "sorted" order.
    This will help explain how to do it: multi variable comparison - Google Search
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to sort linked list alphabetically
    By chickenlittle in forum C++ Programming
    Replies: 9
    Last Post: 09-19-2011, 01:36 AM
  2. Sort A String Alphabetically.
    By wantsree in forum C Programming
    Replies: 2
    Last Post: 02-05-2011, 02:14 AM
  3. how to sort strings alphabetically?
    By jaguar7 in forum C Programming
    Replies: 7
    Last Post: 10-02-2008, 01:32 PM
  4. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  5. How do I heap sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2007, 01:00 AM