Thread: sorting...

  1. #1
    Registered User headexplodes's Avatar
    Join Date
    Aug 2001
    Posts
    26

    sorting...

    hi,

    I have an array of strings and want to be able to sort them alphabetically. I was wondering if someone could point me in the right direction. Are there any functions to assist me with this or do i have to write my own function from scratch?

    thanks, Robert.
    /* MSN and E-Mail - head_explodeshotmail.com */
    If Bill Gates got a dollar for every time windows crahsed... oh... he does.
    I always use MSVC++6.0

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    63
    Hi,
    I think that you would find it easier to make your own function which sorts the array alphabetically.

    My suggested sorting method is Bubble sort, since its short and easy.

    I will experiment with it for a bit and let you know on whether it is successful or unsuccessful.

    I hope you get it working
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well for simple 'C' style arrays, there's a function called qsort() in stdlib.h

    If you're using fancy C++ STL containers, then there is a sort function in <algorithm>

  4. #4
    Registered User headexplodes's Avatar
    Join Date
    Aug 2001
    Posts
    26
    thanks... i'm experimenting with qsort now.
    /* MSN and E-Mail - head_explodeshotmail.com */
    If Bill Gates got a dollar for every time windows crahsed... oh... he does.
    I always use MSVC++6.0

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    63
    go to AI Horizon for all the different methods of sorting.
    You never know when you might need it.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  6. #6
    Registered User headexplodes's Avatar
    Join Date
    Aug 2001
    Posts
    26
    i'm having trouble getting qsort() working...

    Code:
    char* allsongs[MAXSONGS];
    
    int compare( const void *arg1, const void *arg2 )
    {
       /* Compare all of both strings: */
       return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
    }
    
    qsort((void*)allsongs, 10, sizeof(char*), compare);
    this is my modification of the example given in the MSDN. but it gets an access voilation. (btw allsongs is sucessfully filled with about 300 strings)

    if i do the following:

    Code:
    sprintf("%s\n", allsongs[10]);
    it outputs a song fine. have i overlooked somthing?
    /* MSN and E-Mail - head_explodeshotmail.com */
    If Bill Gates got a dollar for every time windows crahsed... oh... he does.
    I always use MSVC++6.0

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    63
    Do yourself a favor and have a look at the bubble sorting method, or another one.

    Just goto aihorizon.com and then click on Basic CS, there is a whole lot of algorithms for sorting.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  8. #8
    Registered User headexplodes's Avatar
    Join Date
    Aug 2001
    Posts
    26
    thanks for all the help.. but i got qsort working. its seems fast enough for what i'm doing.
    /* MSN and E-Mail - head_explodeshotmail.com */
    If Bill Gates got a dollar for every time windows crahsed... oh... he does.
    I always use MSVC++6.0

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    63
    Its always a pleasure sharing some of my knowledge.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  3. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 08:05 PM