sort a string

Steven Schveighoffer schveiguy at gmail.com
Fri May 1 19:25:43 UTC 2020


On 5/1/20 11:17 AM, drug wrote:
> 01.05.2020 18:04, notna пишет:
>>
>> hmmm, whích results in:
>>   Error: cannot use [] operator on expression of type dchar
>>
> 
> try this:
> ```D
> import std;
> void main()
> {
>      string word = "Привет";
>      dchar[] line3 = to!(dchar[])(word.dup) // make a copy to get a 
> range of mutable char
>                                             // and convert char to dchar
>          .sort                              // sort it
>          .release;                          // get the sorted range
>      assert(line3 == "Пвеирт");
> }
> ```

Nice! Yeah, I was sloppy in my newsgroup coding, sorry.

One minor nit here, the to!(dchar[])(word.dup), the dup is not 
necessary, you are going to end up allocating a temporary array and 
throwing it away.

Just do word.to!(dchar[]). `to` takes care of all the formalities.

-Steve


More information about the Digitalmars-d-learn mailing list