digitalmars.D.learn - sort a string
- Chris Katko (13/13) May 01 2020 I'm making anagrams. According to the nextPermutation() docs, I
- drug (14/32) May 01 2020 import std;
- Steven Schveighoffer (6/33) May 01 2020 dchar[] line3 = sort(line2.to!dchar[]).release;
- drug (4/9) May 01 2020 Argh, as always you're right. Funny that I never did that and sadly that...
- notna (3/6) May 01 2020 hmmm, whÃch results in:
- bachmeier (10/20) May 01 2020 Working program:
- notna (3/25) May 01 2020 well, this makes very much sense ;)
- drug (15/19) May 01 2020 try this:
- notna (2/23) May 01 2020 THANKS, this looks even cleaner :)
- Steven Schveighoffer (7/27) May 01 2020 Nice! Yeah, I was sloppy in my newsgroup coding, sorry.
- notna (7/14) May 02 2020 THANK YOU for all the great hints and explanations here and in
- norm (7/20) May 01 2020 You need to convert the sort output to dchar[], e.g.
- Chris Katko (2/10) May 01 2020 That works, thanks!
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
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
On 5/1/20 4:12 AM, drug wrote:01.05.2020 10:38, Chris Katko пишет:dchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.releaseI'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[]));Don't do this, use to!(dchar[]) as you have above. This will create incorrect dchars for non-ascii text. -Steve"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
May 01 2020
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. -SteveArgh, 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
On Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote:hmmm, whÃch results in: Error: cannot use [] operator on expression of type dchardchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.release       dchar[] line3 = sort(line2.to!(dchar[]));
May 01 2020
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: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.hmmm, whÃch results in: Error: cannot use [] operator on expression of type dchardchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.release       dchar[] line3 = sort(line2.to!(dchar[]));
May 01 2020
On Friday, 1 May 2020 at 15:15:29 UTC, bachmeier wrote:On Friday, 1 May 2020 at 15:04:01 UTC, notna wrote:well, this makes very much sense ;) THANKS a lot, works and helped to adopt some old codeOn Friday, 1 May 2020 at 12:29:26 UTC, Steven Schveighoffer wrote: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.hmmm, whÃch results in: Error: cannot use [] operator on expression of type dchardchar[] line3 = sort(line2.to!dchar[]).release; https://dlang.org/phobos/std_range.html#.SortedRange.release       dchar[] line3 = sort(line2.to!(dchar[]));
May 01 2020
01.05.2020 18:04, notna пишет:hmmm, whÃch results in:  Error: cannot use [] operator on expression of type dchartry 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
On Friday, 1 May 2020 at 15:17:53 UTC, drug wrote:01.05.2020 18:04, notna пишет:THANKS, this looks even cleaner :)hmmm, whÃch results in:  Error: cannot use [] operator on expression of type dchartry 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
On 5/1/20 11:17 AM, drug wrote:01.05.2020 18:04, notna пишет: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. -Stevehmmm, whÃch results in:   Error: cannot use [] operator on expression of type dchartry 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
On Friday, 1 May 2020 at 19:25:43 UTC, Steven Schveighoffer wrote: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. -SteveTHANK YOU for all the great hints and explanations here and in general in this NG, Steve! No blaming for the "sloopy code" at all. I should have seen the missing [] by myself, but... _AND_ btw. your hint with the "release" was key to the discussion / solution anyhow! :)
May 02 2020
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
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:That works, thanks![...]You need to convert the sort output to dchar[], e.g. --- dchar[] line3 = sort(line2.to!(dchar[])).to!(dchar[]); --- Cheers, Norm
May 01 2020