Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting arrays by the first n characters

0 views
Skip to first unread message

Philliam Auriemma

unread,
Dec 26, 2009, 3:31:01 PM12/26/09
to
Hey guys,
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

unread,
Dec 26, 2009, 4:42:56 PM12/26/09
to
Philliam Auriemma <phil.a...@gmail.com> wrote:
> Is there any way to sort an array without any regard to the characters
> after an n number of 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?

> I would want to sort an array full of names like that by just the first
> whatever characters, in this case, 9.

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?

regards Henrik
--
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:
root@localhost postmaster@localhost

0 new messages