Thread: Sorting aphabetically?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    Sorting aphabetically?

    I am trying to sort an array of structures alphabetically. Can this be done?

    if yes, how? I can only find sorts that organise numerically. Any help would be appreciated.

    Blue Bob

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    If you're sorting characters or strings thats the same as sorting numbers, since they get converted to their ascii equivalents.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, just read the string in each struct's first character and compare. Simple as that...
    1978 Silver Anniversary Corvette

  4. #4
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Or ...

    Code:
    int strcmp (const char *s1, const char *s2);
    from string.h

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Doesn't that function just tell if the strings are the same or not? Returning only a 0 or 1. Please correct me if I'm wrong...
    1978 Silver Anniversary Corvette

  6. #6
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Check your system's documentation on the qsort() library function. I believe that is what you are after.
    Jason Deckard

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    Originally posted by Garfield
    Doesn't that function just tell if the strings are the same or not? Returning only a 0 or 1. Please correct me if I'm wrong...
    if s1 is less that s2, strcmp will return a negative number. if they are equal, zero is returned. if s1 is greater than s2 strcmp will return a positive number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM