Sorting Strings

This is a discussion on Sorting Strings within the C++ Programming forums, part of the General Programming Boards category; I'm a C++ student in high school and was wondering if someone could help me figure out how to alphabetize ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    3

    Question Sorting Strings

    I'm a C++ student in high school and was wondering if someone could help me figure out how to alphabetize a list of strings like

    ADAM
    JOHN
    BILL
    DENNIS
    LARRY
    ALAN
    BOBBY

    the problem i have is storing them in an array because character arrays hold each letter instead of a whole string.

    help if you can.

    Thanks,
    Blammo14@aol.com

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    just sort per character position, with each position having precedence over the last.
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    3

    Question still confused...

    Yeah i understand that part but i still need to be pointed into the right direction as far as how to write the code...is there a way to store strings in an array...any help would be appreciated.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    oh i see. you can use multiple indirection [which allows, IMHO more flexiblity] or you can use a multi-indexed array.
    hasafraggin shizigishin oppashigger...

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    3

    Thumbs up thanks

    Thanks for your help. If anyone has any other ideas be sure to post, but I will try what has been suggested and let you know how it goes.

    Thanks again!

  6. #6
    Unregistered
    Guest
    An array of strings is a two dimensional char array. Each string can have up to a maximal number of char, but may have fewer. If it has fewer, then the remainder of the unused elements represent "wasted" space (although that usually isn't a major problem). There are other sorting methods than qsort(). One sort that is reasonably easy to write your own code for is a bubble sort, but there are others as well. The key is that sorting c_style strings requires use of strcmp() or one of it's related functions.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Need more info. What if you had first & last names to sort

    This piece of code is pretty good. I have been looking for something quite different. Which codes would you use to sort first and last names instead of just the first name?
    I tried and failed. see my codes.

    -----Configuration: alphabetizeName - Win32 Debug-----------
    Compiling...
    alphabetizeName.cpp
    c:\documents\desktop\badtest\alphabetizename.cpp(5 6) : error C2059: syntax error : ')'
    Error executing cl.exe.
    alphabetizeName.obj - 1 error(s), 0 warning(s)

    The codes:
    /*
    first tested 2/2/02
    Purpose: Sort in alphabetical order a list of strings of string array.
    */

    #include <iostream>
    #include <cstring>
    using namespace std;
    #define const elems 12
    void showArray(char strings[][17], int);

    int main()
    {
    char (strings[12][17]) = {
    "Carl Billion", "Smith Birt", "Al Jim",
    "Albert Jim", "Stan Karty", "Rose Amre",
    "Bay Terr", "Johnson Erab", "Allison Carr",
    "Jean Joey", "Will Bill", "James Gart"
    };
    cout << "The unsorted names are: \n\n";
    showArray(strings, 12);


    qsort( strings, 12, 17, (int (*) ( const void*, const void*) )strcmp);
    for( int i = 0; i < 12; i++ )
    // puts( strings[i] );
    cout << (strings[i]) << endl;
    return 0;
    }


    // function definitions ==========================
    void showArray(char strings[12][17], int elems)
    {
    for (int i = 0; i < elems; i++)
    {
    for ( int j = 0 ; j < elems; j++)
    cout << strings[i][j]<<" ";
    cout << endl;
    }

    cout<<endl<<endl;
    }

  8. #8
    Unregistered
    Guest
    One approach to sorting names by last name is to break the string containing both the first and last names into two strings, one containing first name and one containing last name and storing that information in a struct or class where the struct is defined something like this;

    struct names
    {
    char firstName[30];
    char lastName[30];
    int age;
    char gender;
    }

    If you place a number of names in a container such as an array or a list you can sort by any "field" used in the struct declaration. In fact, with a little practice you can subsort by last name, then by first name, then by age.

    To break up the full name string into two substrings you can write your own (parsing) function or look up strtok().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 10:02 PM
  2. strings, sorting
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 02-18-2006, 04:29 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. bubble sorting multiple strings
    By jibbles in forum C Programming
    Replies: 10
    Last Post: 10-12-2003, 12:48 AM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 01:17 PM

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