On Wednesday, 6 December 2017 at 09:25:20 UTC, Biotronic wrote:
On Wednesday, 6 December 2017 at 08:59:09 UTC, Fredrik Boulund wrote:
string word = "longword";
writeln(sort(word));

But that doesn't work because I guess a string is not the type of range required for sort?

Yeah, narrow (non-UTF-32) strings are not random-access, since characters like 💩 take up more than one code unit, and so "💩"[0] returns an invalid piece of a character instead of a full character.

In addition, sort does in-place sorting, so the input range is changed. Since D strings are immutable(char)[], changing the elements is disallowed. So in total, you'll need to convert from a string (immutable(char)[]) to a dchar[]. std.conv.to to the rescue:

    import std.stdio : writeln;
    import std.conv : to;
    import std.algorithm.sorting : sort;

    string word = "longword";
    writeln(sort(word.to!(dchar[]))); // dglnoorw

--
  Biotronic

Or you simply do
----
writeln("longword".array.sort);
----

Reply via email to