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
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
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.
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: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
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.
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.
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!