sort a string

Steven Schveighoffer schveiguy at gmail.com
Fri May 1 12:29:26 UTC 2020


On 5/1/20 4:12 AM, drug wrote:
> 01.05.2020 10:38, Chris Katko пишет:
>> I'm making anagrams. According to the nextPermutation() docs, I need 
>> to 'sort by less' to get all permutations. ... Except the doc page 
>> doesn't mention how to do that, nor does std.algorithm.sort show how 
>> to sort a string. ... and the google results on the dlang forums from 
>> 2017 don't work.
>>
>> I've tried .byCodeUnit. , .representation. I've tried sorting on the 
>> dchar. I've tried sorting the on string.
>>
>> The closest I've gotten:
>>
>>      string word = "bar";
>>      string line2 = toLower!(string)(word);
>>         dchar[] line3 = sort(line2.to!(dchar[]));

dchar[] line3 = sort(line2.to!dchar[]).release;

https://dlang.org/phobos/std_range.html#.SortedRange.release


>>
>> "Error: cannot implicitly convert expression sort(to(line2)) of type 
>> SortedRange!(dchar[], "a < b") to dchar[]"
>>
> 
> import std;
> void main()
> {
>      string word = "bar";
>      dchar[] line3 = word.dup // make a copy to get a range of mutable 
> elements
>          .map!"dchar(a)" // convert char to dchar

Don't do this, use to!(dchar[]) as you have above. This will create 
incorrect dchars for non-ascii text.

-Steve


More information about the Digitalmars-d-learn mailing list