Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6004485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:16:11+00:00 2026-05-23T01:16:11+00:00

Based on this post, I am trying to understand how sort_by works in JSON::PP

  • 0

Based on this post, I am trying to understand how sort_by works in JSON::PP.

When I run this code

#!/usr/bin/perl
use strict;
use warnings;
use JSON::PP;
use Data::Dumper qw(Dumper);

my $h = {
    22 => { title => "c", name => "d" },
    1  => { title => "1", name => "a" },
    10 => { title => "a", name => "c" },
    5  => { title => "b", name => "b" },
};

my $sorter = sub {
    # See what's going on.
    print "$JSON::PP::a cmp $JSON::PP::b\n";
    print Dumper(\@_, $_);
    <STDIN>; # press return to continue

    $JSON::PP::a cmp $JSON::PP::b
};

my $js = JSON::PP->new;
my $output = $js->sort_by($sorter)->encode($h);
print $output . "\n";

it first sorts the inner keys, and then the outer keys, which determines the final order in the JSON string.

Right now it outputs

{"1":{"name":"a","title":"1"},"10":{"name":"c","title":"a"},"22":{"name":"d","title":"c"},"5":{"name":"b","title":"b"}}

and what I would like to end up with is that it is sorted by title ie.

{"1":{"name":"a","title":"1"},"5":{"name":"b","title":"b"}"10",{"name":"c","title":"a"},"22":{"name":"d","title":"c"}}

I suppose the first problem is to disable the last outter key sort?

Then how do I get hold of the value of title? When the algorithm runs, $JSON::PP::a and $JSON::PP::b contains the value name and title from the same hash.

This I can’t figure out. Can anyone explain this, and/or help me write this algorithm?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:16:12+00:00Added an answer on May 23, 2026 at 1:16 am

    You can’t, or at least not easily. The function you give to sort_by only has access to the keys being sorted. In order to do what you want you’d need to have access to the values associated with those keys (or, more likely, the hashref to which the keys belong, so you could look up the values yourself). That seems like it would be a useful enhancement; you might submit a feature request.

    If your data structure is simple enough (which your example seems to be), you could keep a reference to the hash yourself. In order to do that, you have to be able to distinguish the keys in the inner hashes from the keys in the outer hashes (so you know which hash is being sorted, and thus what kind of comparison to do).

    my $sorter = sub {
        if ($JSON::PP::a =~ /^\d+$/) {
          return $h->{$JSON::PP::a}{title} cmp $h->{$JSON::PP::b}{title};
        }
        return $JSON::PP::a cmp $JSON::PP::b
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is someone elses code that I am trying to understand and modify... The
I'm trying to create a generic LINQ-TO-SQL repository based on this post which basically
I'm trying to understand how to use WCF Data Services (based on EF 4.1)
i found this code online and im trying to compile it, but gcc keeps
Based on this question it appears that the default template for CheckStyle will allow
Based on this question I don't want to litter my ready stuff waiting for
I want to create dynamic content based on this. I know it's somewhere, as
I have up to 4 files based on this structure (note the prefixes are
I'm building an ogre3d tutorial based on this setup http://www.ogre3d.org/wiki/index.php/SettingUpAnApplication#XCode I've followed all steps
I have been writting a keyword search script based on this tutorial: http://www.hackosis.com/2007/11/06/howto-simple-search-engine-with-php-and-mysql/ Like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.