On Mon, 12 May 2014 11:08:47 -0700
Charles Hixson via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> wrote:

> On 05/12/2014 09:29 AM, Jonathan M Davis via Digitalmars-d-learn
> wrote:
> > On Mon, 12 May 2014 14:49:52 +0000
> > hane via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> > wrote:
> >
> >> and is there any way to sort char array with algorithm.sort?
> >> ---
> >> import std.algorithm;
> >> import std.range;
> >>
> >> void main()
> >> {
> >>     int[] arr = [5, 3, 7];
> >>     sort(arr); // OK
> >>
> >>     char[] arr2 = ['z', 'g', 'c'];
> >>     sort(arr2); // error
> >>     sort!q{ a[0] > b[0] }(zip(arr, arr2)); // error
> >> }
> >> ---
> >> I don't know what's difference between int[] and char[] in D, but
> >> it's very unnatural.
> > All strings in D are treated as ranges of dchar, not their element
> > type. This has to with the fact that a char or wchar are only part
> > of a character. If you want to sort arrays of characters, you need
> > to use dchar[].
> >
> > http://stackoverflow.com/questions/12288465
> > http://stackoverflow.com/questions/16590650
> >
> > - Jonathan M Davis
> >
> Given that he was working with pure ASCII, he should be able to cast
> the array to byte[] and sort it, but I haven't tried.

Sure, you can cast char[] to ubyte[] and sort that if you know that the array
only holds pure ASCII. In fact, you can use std.string.representation to do it
- e.g.

auto ascii = str.representation;

and if str were mutable, then you could sort it. But that will only work if
the string only contains ASCII characters. Regardless, he wanted to know why
he couldn't sort char[], and I explained why - all strings are treated as
ranges of dchar, making it so that if their element type is char or wchar, so
they're not random access and thus can't be sorted.

> Also char[] isn't string.

Yes, string is aliased to immutable(string)[], but char[] is still a string
type, and what I said applies to all string types. In particular, arrays of
char or wchar are called "narrow strings," because it's not guaranteed that
one of their code units is a full code point, unlike arrays of dchar. But all
arrays of char, wchar, or dchar are strings.

> Strings are immutable, and thus cannot be sorted in place.

"string" is immutable and thus couldn't be sorted regardless of whether narrow
strings were treated as ranges of dchar or not, but char[] is a string just as
much as immutable(char)[] is. It's just not string.

- Jonathan M Davis

Reply via email to