www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Print int[string] sorted by Value

reply Paul <phshaffer gmail.com> writes:
per the D sample wc2.d....
size_t[string] dictionary;  <-is printed by...
.....
foreach (word1; dictionary.keys.sort) writef ....etc

I want to print the dictionary sorted by value not key.  I can 
write an algorithm but is there a library method(s) I can use to 
iterate through the array sorted by decreasing values?

Thanks for your time.
Oct 28 2020
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Wednesday, 28 October 2020 at 15:15:40 UTC, Paul wrote:
 per the D sample wc2.d....
 size_t[string] dictionary;  <-is printed by...
 .....
 foreach (word1; dictionary.keys.sort) writef ....etc

 I want to print the dictionary sorted by value not key.  I can 
 write an algorithm but is there a library method(s) I can use 
 to iterate through the array sorted by decreasing values?

 Thanks for your time.
import std.array, std.algorithm; auto sorted = dictionary.byPair.array.sort!((a, b) => a.value < b.value)
Oct 28 2020
next sibling parent Paul <phshaffer gmail.com> writes:
On Wednesday, 28 October 2020 at 15:25:26 UTC, Paul Backus wrote:
 On Wednesday, 28 October 2020 at 15:15:40 UTC, Paul wrote:
 per the D sample wc2.d....
 size_t[string] dictionary;  <-is printed by...
 .....
 foreach (word1; dictionary.keys.sort) writef ....etc

 I want to print the dictionary sorted by value not key.  I can 
 write an algorithm but is there a library method(s) I can use 
 to iterate through the array sorted by decreasing values?

 Thanks for your time.
import std.array, std.algorithm; auto sorted = dictionary.byPair.array.sort!((a, b) => a.value < b.value)
Thanks Paul
Oct 28 2020
prev sibling parent Paul <phshaffer gmail.com> writes:
On Wednesday, 28 October 2020 at 15:25:26 UTC, Paul Backus wrote:
 auto sorted = dictionary.byPair.array.sort!((a, b) => a.value < 
 b.value)
It seems this method produces a ?sorted array of tuples? [..Tuple!(string, "key", uint, "value")("Program", 74), Tuple!(string, "key", uint, "value")("rd", 74)..] I guess I would just have to iterate through the tuples.
Oct 28 2020
prev sibling next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Oct 28, 2020 at 03:15:40PM +0000, Paul via Digitalmars-d-learn wrote:
 per the D sample wc2.d....
 size_t[string] dictionary;  <-is printed by...
 .....
 foreach (word1; dictionary.keys.sort) writef ....etc
 
 I want to print the dictionary sorted by value not key.  I can write
 an algorithm but is there a library method(s) I can use to iterate
 through the array sorted by decreasing values?
[...] Just use a different sorting predicate: import std; void main() { int[string] aa = [ "abc": 321, "def": 234, "ghi": 524, "jkl": 310, "mno": 110, "pqr": 910, ]; foreach (key; aa.keys.sort!((a,b) => aa[a] < aa[b])) { writeln(key); } } T -- Curiosity kills the cat. Moral: don't be the cat.
Oct 28 2020
next sibling parent Paul <phshaffer gmail.com> writes:
Thanks Teoh
Oct 28 2020
prev sibling parent Paul <phshaffer gmail.com> writes:
On Wednesday, 28 October 2020 at 15:27:04 UTC, H. S. Teoh wrote:

 		foreach (key; aa.keys.sort!((a,b) => aa[a] < aa[b])) {
 			writeln(key);
This solution worked perfectly without modifying any of my other code. I don't fully understand it but can study up on the syntax.
Oct 28 2020
prev sibling parent reply aberba <karabutaworld gmail.com> writes:
On Wednesday, 28 October 2020 at 15:15:40 UTC, Paul wrote:
 per the D sample wc2.d....
 size_t[string] dictionary;  <-is printed by...
 .....
 foreach (word1; dictionary.keys.sort) writef ....etc

 I want to print the dictionary sorted by value not key.  I can 
 write an algorithm but is there a library method(s) I can use 
 to iterate through the array sorted by decreasing values?

 Thanks for your time.
Have you tries .values() function? dictionary.values.sort()
Oct 28 2020
parent reply Paul <phshaffer gmail.com> writes:
On Wednesday, 28 October 2020 at 15:40:23 UTC, aberba wrote:
 Have you tries .values() function? dictionary.values.sort()
Thanks aberba. Yes, that was my first attempt! If my terminology is correct that gives me a "range" of sorted VALUES. I think I can't "iterate"(foreach) through an array of VALUE[KEY] using the VALUE. I can only iterate over the KEYS...maybe? If my array is of type int[string] I can do: foreach(word-STRING, range-of-STRINGS) writeln(dictionary[word-STRING]); but not: foreach(value-INT, range-of-VALUES) writeln(dictionary[value-INT]<- wrong type);
Oct 28 2020
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/28/20 9:30 AM, Paul wrote:

 On Wednesday, 28 October 2020 at 15:40:23 UTC, aberba wrote:
 Have you tries .values() function? dictionary.values.sort()
Thanks aberba. Yes, that was my first attempt! If my terminology is correct that gives me a "range" of sorted VALUES.
No, both .values and .keys return dynamic arrays that are freshly populated. .byKey, .byValue, and .byKeyValue are "ranges" that iterate the elements lazily. Ali
Oct 28 2020