Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6: Sorting Hash by Values and using kv

I'm looking at the following Data (in JSON)

{
  "FValidation_pipelineMTHXT_v4.5.1_refLibV2": "concordance2/f",
  "FValidation_pipelineLPJL": "concordance2/c",
  "FCompetenceRuns": "concordance2/b",
  "FWGS": "concordance2/a",
  "Falidation_pipelineMTHXT": "concordance2/e",
  "FValidation_pipelineLPJL_v4.5.1_refLibV2": "concordance2/d"
}

and I'm trying to sort by the hash value

for %files.kv -> $key, $value {

gives the required data, but I want it sorted. I've tried about 20 different methods, which don't work

for %files.sort.kv  -> ($key, $value) {

and

for %files.sort: *.value.kv  -> ($key, $value) {

which was inspired from https://docs.perl6.org/routine/sort#(Map)_method_sort and on and on but none are working :(

How can I sort this hash by value?

like image 713
con Avatar asked May 09 '19 20:05

con


2 Answers

.kv returns a flat sequence.

my %h = (
  a => 3,
  b => 2,
  c => 1,
);

say %h.kv.perl;
# ("a", 3, "c", 1, "b", 2).Seq

If you sort it, then you do so without keeping the key with its associated value.

say %h.kv.sort.perl;
# (1, 2, 3, "a", "b", "c").Seq

So you want to sort it before splitting up the pairs.

# default sort order (key first, value second)
say %h.sort.perl;
# (:a(3), :b(2), :c(1)).Seq

say %h.sort: *.value;       # sort by value only (tied values are in random order)
# (:c(1), :b(2), :a(3)).Seq
say %h.sort: *.invert;      # sort by value first, key second
# (:c(1), :b(2), :a(3)).Seq
say %h.sort: *.kv.reverse;  # sort by value first, key second
# (:c(1), :b(2), :a(3)).Seq

Once it is sorted you can take it as a sequence of Pair objects:

# default $_
for %h.sort: *.invert {
  say .key ~ ' => ' ~ .value
}

# extract as named attributes
for %h.sort: *.invert -> (:$key, :$value) {
  say "$key => $value"
}

# more explicit form of above
for %h.sort: *.invert -> Pair $ (:key($key), :value($value)) {
  say "$key => $value"
}

Or you could pull apart the pairs after the sort:
(Notice the two-level structure.)

say %h.sort(*.invert).map(*.kv).perl;
# (("c", 1).Seq, ("b", 2).Seq, ("a", 3).Seq).Seq
say %h.sort(*.invert)».kv.perl;
# (("c", 1).Seq, ("b", 2).Seq, ("a", 3).Seq).Seq

# default $_
for %h.sort(*.invert).map(*.kv) {
  say .key ~ ' => ' ~ .value
}

# extract inner positional parameters
for %h.sort(*.invert).map(*.kv) -> ($k,$v) {
  say "$k => $v"
}

# `».kv` instead of `.map(*.kv)`
for %h.sort(*.invert)».kv -> ($k,$v) {
  say "$k => $v"
}

You can even flatten it after pulling apart the pair objects.

say %h.sort(*.invert).map(*.kv).flat.perl;
# ("c", 1, "b", 2, "a", 3).Seq
say %h.sort(*.invert)».kv.flat.perl;
# ("c", 1, "b", 2, "a", 3).Seq

for %h.sort(*.invert).map(*.kv).flat -> $k, $v {
  say "$k => $v"
}

for %h.sort(*.invert)».kv.flat -> $k, $v {
  say "$k => $v"
}

(Note that ».method only maps over one method call. To map over two you need ».method1».method2, or just use map .map(*.method1.method2).
So in the ».kv.flat above, only the .kv method is mapped over the values.)

like image 73
Brad Gilbert Avatar answered Oct 11 '22 05:10

Brad Gilbert


my %hash = :a<z>, :b<y>, :c<x>;

for %hash.sort(*.value) {
    say $_.key;
    say $_.value;
}
like image 33
ugexe Avatar answered Oct 11 '22 05:10

ugexe