Thread: Sorting strings like humans do?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Talking Sorting strings like humans do?

    Dear forum!

    I wonder if there is some smart code that will sort strings in alfabetical order in a human way?

    Example:

    Human way...

    name1
    name10
    name12

    Computer way...

    name10
    name12
    name1

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Im not sure what you mean by computer way. Any sorting done is done by humans originally. If you wanted to sort that list like you showed you could probably use strstr, assuming the word was always "name". After finding the word you could skip to the element after the e and check for a digit etc...

    If the names were all different, you could still do it, you would just have to check each element. Your best bet is to look through string.h and learn to use every single function, and then start reading code for nice examples of how its done, and find out which one is more efficient and easier to read.

    Recently I made a function that does something similiar but not the same. Anyways part of that function just searches string till it finds a digit, you could make something like that and just keep reading till there are no more digits, then put them in order yourself. There is "smart code" like more complex algorithms im sure but for something like that you wouldn't need anything really advanced. Goodluck.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    63

    Re: Sorting strings like humans do?

    Originally posted by electrolove
    Dear forum!

    I wonder if there is some smart code that will sort strings in alfabetical order in a human way?

    Example:

    Human way...

    name1
    name10
    name12

    Computer way...

    name10
    name12
    name1
    The strings are not the same, it was just an example, in fact the strings are often made of only chars, not numbers but if there are numbers I want that string to be sorted in the right 'human' way. One more example:

    Human
    String
    Sorting
    Like
    Do
    Like10Strings
    Same
    Like1String

    And so on...

    Any sample code somewhere? I do not want to invent the wheel again... ;-)
    You cantīt teach an old dog new tricks.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    So you would want it to sort:

    Human
    String
    Sorting
    Like
    Do
    Like10Strings
    Same
    Like1String


    into:

    Do
    Human
    Like
    Like1String
    Like10Strings
    Same
    Sorting
    String


    If so, you could use strstr to check for the case of "Like" and manually place the elements in the order you want (assuming you know ahead of time what the elements are). Or just do it the "computer" way, copy the offending elements into another list, reverse said list, then reinsert the elements.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Just use the strcmp() function, here is the prototype.

    int strcmp( const char *s1, const char *s2);

    This compares the string s1 with the string s2. It returns 0 if s1 is equal to s2, less than 0 if it s1 is less than s2, and greater than 0 if s1 is greater than s2, that is on the ascii number chart, according to each character in the string. So, this:

    This
    Will
    Be
    Sorted
    Like
    This.
    ______________

    Be
    Like
    Sorted
    This
    This.
    Will

    If you do something like, if (strcmp(string1, nextstring) > 0){swap code.....};

    P.S. Put that compare in a bubble sort. This works both ways, if you want it the opposite (computers way as you put it) then make it < 0, or the human way is > 0.
    The keyboard is the standard device used to cause computer errors!

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, 09: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-11-2003, 11:48 PM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 12:17 PM