www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sort a string

reply Chris Katko <ckatko gmail.com> writes:
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
next sibling parent reply drug <drug2004 bk.ru> writes:
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
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
next sibling parent drug <drug2004 bk.ru> writes:
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
prev sibling parent reply notna <notna.remove.this ist-einmalig.de> writes:
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
next sibling parent reply bachmeier <no spam.net> writes:
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
parent notna <notna.remove.this ist-einmalig.de> writes:
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
May 01 2020
prev sibling parent reply drug <drug2004 bk.ru> writes:
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
next sibling parent notna <notna.remove.this ist-einmalig.de> writes:
On Friday, 1 May 2020 at 15:17:53 UTC, 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 == "Пвеирт"); } ```
THANKS, this looks even cleaner :)
May 01 2020
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
May 01 2020
parent notna <notna.remove.this ist-einmalig.de> writes:
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.

 -Steve
THANK 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
prev sibling parent reply norm <norm.rowtree gmail.com> writes:
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
parent Chris Katko <ckatko gmail.com> writes:
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