Thread: Sorting list of names?

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

    Question Sorting list of names?

    Ppl,
    I am attempting to sort a list of names..how could I go about that?
    A

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Use your favorite sorting method except with strcmp for the comparisions and strncpy for the swapping.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    Umm..what would some of the code look like?
    A
    P.S: Thanks already!

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Umm..what would some of the code look like?
    It depends on what the list of names looks like. Is it a linked list or an array of pointers? But I get the feeling that you will find plenty of code by searching these boards for sorting keywords. It saves me the trouble of writing a new sorting routine or scrounging around for one of the ones I've collected.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    This is what I have so far..

    Hi,
    This is what I've got so far..but for some reason before it sets the entire list it crashes..I have 16 names i.e rows = 16..someone please help debug


    void sortLastNames(char** strings,
    int rows)
    {
    /* Local Definitions */
    int i;
    char temp;

    printf("SORTED STRINGS:\n");
    for(i=0; i < rows; i++)
    {
    if(strcmp(strings[i], strings[i+1])>0)
    {
    temp = strings[i];
    strings[i] = strings[i+1];
    strings[i+1] = temp;
    }

    printf("%s\n", strings[i]);
    }
    //end of sort
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you should pay attention to your compiler's warning messages.

    > temp = strings[i];
    You should be getting warnings about this

    Make
    char *temp;
    to fix this

    > strcmp(strings[i], strings[i+1])>
    The i+1 ensures you step off the end of the array when you do the last iteration of the loop

    > void sortLastNames(char** strings
    Is this really the type of the array you're passing to this function.

    char *strings[10]; // good
    char strings[10][10];// bad

    One more thing, this only sorts the last element of the array
    Search the web for more detail on how to implement bubble sorts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM