Skip to content

Instantly share code, notes, and snippets.

@jaguart
Last active September 13, 2022 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaguart/bd64157bcfa8271ad5bed69a286e0613 to your computer and use it in GitHub Desktop.
Save jaguart/bd64157bcfa8271ad5bed69a286e0613 to your computer and use it in GitHub Desktop.
ordered hash for JSON::Fast and META6
#!/usr/bin/env raku
use v6;
use JSON::Fast;
class MyHash is Hash {
my %w = (
'first' => ' ',
'perl' => ' a',
'raku' => ' b',
'name' => ' c',
'auth' => ' d',
'version' => ' e',
'api' => ' f',
'description' => ' g',
'source-url' => ' h',
'tags' => ' i',
'authors' => ' j',
'license' => ' k',
'provides' => ' l',
'depends' => ' m',
'resources' => ' n',
'last' => '~ ',
'final' => '~~',
);
sub by-weighted-keys {
(%w{$^a.key} // $^a.key) cmp (%w{$^b.key} // $^b.key);
}
method sort( *$keys --> Seq:D ) {
return sort( &by-weighted-keys, self.pairs );
}
}
my %x := MyHash.new: {
"perl" => "6.c",
"name" => "Vortex::TotalPerspective",
"api" => "1",
"auth" => "github:SomeAuthor",
"version" => "0.0.1",
"description" => "Wonderful simulation to get some perspective.",
"authors" => [ "Your Name" ],
"license" => "Artistic-2.0",
"provides" => {
"Vortex::TotalPerspective" => "lib/Vortex/TotalPerspective.rakumod"
},
"depends" => [ ],
"build-depends" => [ ],
"test-depends" => [ ],
"resources" => [ ],
"tags" => [
"Vortex", "Total", "Perspective"
],
"source-url" => "git=>//github.com/you/Vortex-TotalPerspective.git"
};
say to-json( %x, :sorted-keys );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment