sort a string
drug
drug2004 at bk.ru
Fri May 1 08:12:25 UTC 2020
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[]));
>
> "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
.array // convert range to random access range (dynamic array
here) to enable sorting
.sort // sort
.array; // convert SortedRange to dynamic array
assert(line3 == "abr");
}
More information about the Digitalmars-d-learn
mailing list