why do you need a custom function byvalue() ?

Even sorting the keys of a normal hash by value¹ can only be done by somehow tunneling the reference into byvalue() by closure/currying (like choroba showed) or by doing a (schwarzian?) transform of the hash into an array of key/value-tupels.

Both approaches look like overkill if you can do this straightforwardly:

my %h = %{$self->{hash_ref}}; my @keys = sort { $h{$a} <=> $h{$b} } keys %h;

Easy to read, easy to understand.

So why ?

DB<100> $self={ hash_ref=>{item1=>'value1',item2=>'value2'} } => { hash_ref => { item1 => "value1", item2 => "value2" } } DB<101> %h=%{$self->{hash_ref}} => ("item1", "value1", "item2", "value2") DB<103> @keys=sort {$h{$a}<=>$h{$b}} keys %h; => ("item1", "item2")

Cheers Rolf

PS: Je suis Charlie!

PS: downvoted for lack of indentation, I agree with the Guido comment.

¹) hint "Reduction to a simpler problem"!


In reply to Re: How can I access object data in a user defined sort cmp function? by LanX
in thread How can I access object data in a user defined sort cmp function? by tkguifan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.