Sort characters in string
Fredrik Boulund
fredrik.boulund at gmail.com
Wed Dec 6 10:13:53 UTC 2017
On Wednesday, 6 December 2017 at 09:25:20 UTC, Biotronic wrote:
> 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
Also very useful information! Thanks. I was just realizing that
sort was in-place as I finished writing my first post. It got me
really confused as I expected it to return a sorted array (but I
do realize that is a strange assumption to make).
More information about the Digitalmars-d-learn
mailing list