Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 01, 2020 sort a string | ||||
---|---|---|---|---|
| ||||
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[]" |
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Katko | 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");
}
|
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Katko | On Friday, 1 May 2020 at 07:38:53 UTC, Chris Katko wrote: > 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[]" You need to convert the sort output to dchar[], e.g. --- dchar[] line3 = sort(line2.to!(dchar[])).to!(dchar[]); --- Cheers, Norm |
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to norm | On Friday, 1 May 2020 at 08:17:33 UTC, norm wrote:
> On Friday, 1 May 2020 at 07:38:53 UTC, Chris Katko wrote:
>> [...]
>
> You need to convert the sort output to dchar[], e.g.
> ---
> dchar[] line3 = sort(line2.to!(dchar[])).to!(dchar[]);
> ---
>
> Cheers,
> Norm
That works, thanks!
|
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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 |
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 01.05.2020 15:29, Steven Schveighoffer пишет:
>
> Don't do this, use to!(dchar[]) as you have above. This will create incorrect dchars for non-ascii text.
>
> -Steve
Argh, as always you're right. Funny that I never did that and sadly that I posted wrong code. Thank you, Steven, for correction of my wrong posts, I appreciate it.
|
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote:
>>> dchar[] line3 = sort(line2.to!(dchar[]));
>
> dchar[] line3 = sort(line2.to!dchar[]).release;
>
> https://dlang.org/phobos/std_range.html#.SortedRange.release
>
hmmm, whích results in:
Error: cannot use [] operator on expression of type dchar
|
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to notna | On Friday, 1 May 2020 at 15:04:01 UTC, notna wrote: > On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote: > >>>> dchar[] line3 = sort(line2.to!(dchar[])); >> >> dchar[] line3 = sort(line2.to!dchar[]).release; >> >> https://dlang.org/phobos/std_range.html#.SortedRange.release >> > > hmmm, whích results in: > Error: cannot use [] operator on expression of type dchar Working program: import std.algorithm, std.conv, std.string, std.stdio; void main() { string word = "bar"; string line2 = toLower!(string)(word); dchar[] line3 = sort(line2.to!(dchar[])).release; writeln(line3); } You need to add parens. |
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to notna | 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 == "Пвеирт");
}
```
|
May 01, 2020 Re: sort a string | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | On Friday, 1 May 2020 at 15:15:29 UTC, bachmeier wrote:
> On Friday, 1 May 2020 at 15:04:01 UTC, notna wrote:
>> On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote:
>>
>>>>> dchar[] line3 = sort(line2.to!(dchar[]));
>>>
>>> dchar[] line3 = sort(line2.to!dchar[]).release;
>>>
>>> https://dlang.org/phobos/std_range.html#.SortedRange.release
>>>
>>
>> hmmm, whích results in:
>> Error: cannot use [] operator on expression of type dchar
>
> Working program:
>
> import std.algorithm, std.conv, std.string, std.stdio;
>
> void main() {
> string word = "bar";
> string line2 = toLower!(string)(word);
> dchar[] line3 = sort(line2.to!(dchar[])).release;
> writeln(line3);
> }
>
> You need to add parens.
well, this makes very much sense ;)
THANKS a lot, works and helped to adopt some old code
|
Copyright © 1999-2021 by the D Language Foundation