Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Sorting arrays by the first n characters
- Philliam Auriemma
December 26, 2009, 8:31 pm
rate this thread
Is there any way to sort an array without any regard to the characters
after an n number of characters? What I wanted to do was save a file
with the name being the date("U"); and then have a short string
appended to the end of that. So a possible file name would be like,
123978402-asdf.txt, and I would want to sort an array full of names
like that by just the first whatever characters, in this case, 9.
- Henrik Carlqvist
December 26, 2009, 9:42 pm
Re: Sorting arrays by the first n characters
Why? If you don't care about the last characters, why would you bother if
also that part got sortet correctly instead of in some arbitary order?
To compare two complete strings the correct way you would probably use
strcmp. To only compare up to 9 characters you could instead use strncmp,
but again, why?
In most cases when comparing a lot of strings strncmp will probably be
slower than strcmp. What would the gain be to maybe get a little
randomness in the result instead of a total predictable result?
The address in the header is only to prevent spam. My real address is:
hc3(at)poolhem.se Examples of addresses which go to spammers: