G
Grehom
what am I doing wrong? I wanted to sort primarily on count (second
position in array), then sub-sort alphabetically (first position in
array)
char_freq = [["c", 2],["b", 5],["a", 2]]
sorted_freq = char_freq.sort {|a, b| b[1]<=>a[1] || a[0]<=>b[0] }
sorted_freq.each do |d|
print d[0]*d[1]
end
produces >> bbbbbccaa
when I was rather hoping for >> bbbbbaacc
I tried using brackets and using 'or' instead of '||' even tried the
sort_by method, any help much appreciated I know this works in perl
(add 13 dollar signs, etc and shake well!):
use strict;
use warnings;
my @char_freq = (["c", 2], ["b", 5], ["a", 2]);
foreach my $d (sort {$$b[1] <=> $$a[1] || $$a[0] cmp $$b[0] }
@char_freq) {
print $$d[0] x $$d[1]
}
produces >> bbbbbaacc
position in array), then sub-sort alphabetically (first position in
array)
char_freq = [["c", 2],["b", 5],["a", 2]]
sorted_freq = char_freq.sort {|a, b| b[1]<=>a[1] || a[0]<=>b[0] }
sorted_freq.each do |d|
print d[0]*d[1]
end
produces >> bbbbbccaa
when I was rather hoping for >> bbbbbaacc
I tried using brackets and using 'or' instead of '||' even tried the
sort_by method, any help much appreciated I know this works in perl
(add 13 dollar signs, etc and shake well!):
use strict;
use warnings;
my @char_freq = (["c", 2], ["b", 5], ["a", 2]);
foreach my $d (sort {$$b[1] <=> $$a[1] || $$a[0] cmp $$b[0] }
@char_freq) {
print $$d[0] x $$d[1]
}
produces >> bbbbbaacc