sort(charArray) doesn't work
Jonathan M Davis
jmdavisProg at gmx.com
Wed Nov 13 10:16:27 PST 2013
On Wednesday, November 13, 2013 11:32:18 Uplink_Coder wrote:
> sort doesn't work on an primitive Array ?
> Is that normal ....
> please look @http://www.dpaste.dzfl.pl/961286e1
> because I don't know what i'm doing worng
>
> Thanks in Advance :D
Strings are treated as ranges of dchar, so if the element types is char or
wchar, they're not random-access (since the number of code units per code
point varies, meaning that each element in the array is a piece of a character
and not necessarily an entire character), and sort requires random access. So,
if you want to properly sort the string, you need to convert it to a random-
access range - the mostly likely choice being dchar[], which you could do with
to!(dchar[])(charArray), but of course, if you then want char[] again, you'd
need to convert it back, e.g. to!(char[])(sort(to!(dchar[])(charArray))).
However, if you're _certain_ that all of the characters are ASCII and
therefore don't take more than one char, you can convert the string to ubyte[]
and sort that. std.string.representation will do that for you. e.g.
sort(representation(charArray)). But that _will_ mangle Unicode strings, so
you need to be sure that your string only contains ASCII characters.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list