Sorting char arrays
Jonathan M Davis
jmdavisProg at gmx.com
Mon Mar 12 10:24:56 PDT 2012
On Monday, March 12, 2012 16:04:37 Magnus Lie Hetland wrote:
> On 2012-03-12 13:09:18 +0000, Dmitry Olshansky said:
> > Mm it should perform sort on UTF-8 buffer?
>
> Humm -- dunno ;) The UTF-8-semantics of single characters sort of
> slipped my mind :)
>
> > Tricky thing but worths an enhancement request.
>
> I'm just thinking an array of anything that can be compared should
> probably be sort-able. But comparing individual UTF-8-bytes can be
> weird, indeed. So, yeah. I guess the weirdness follows from the fact
> that individual characters are considered UTF-8 :)
>
> > If it's ASCII then try:
> > sort(a.representation)
> >
> > representation is in std.string IRC.
>
> The thing is, I'm using sort() in a template, and I just happen to use
> char as the template parameter in a unit test. And since I have no
> special UTF-8 values in there, my own sort() works just fine. (Although
> maybe it shouldn't? ;)
The problem is that sort requires a random access range, and narrow string
(string and wstring) aren't, because they're variable length encoded. I'm not
sure that strings _can_ be efficiently sorted, because of that, but maybe
there's a sorting algorthm that could do it reasonably efficiently, and we could
special case sort for narrow strings to use that one, but it's a while since I
messed with sorting algorithms, so I don't remember all of their
characteristics off of the top of my head. Certainly, with how sort is currenly
implemented, it can't handle any range which isn't a random access range.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list