json_fast icon indicating copy to clipboard operation
json_fast copied to clipboard

In README show how to get "to-json" keys sorted like "%h.keys.sort({.Numeric)"

Open tbrowder opened this issue 2 years ago • 1 comments

tbrowder avatar Jul 12 '23 17:07 tbrowder

This works (as suggested by [Coke]): define a sub suitable for the key types expected:

sub my-sort($a, $b) { $a.key cmp $b.key }
my $jstr = to-json %h, :pretty, :sorted-keys(&my-sort);

If multiple key types are expected, then the sub should use elsif or when to define what the user wants when a key pair is not directly comparable. For example, I used this sub for mixed Str and Int types:

TO BE COMPLETE A BIT LATER (when I get back to a real computer)

tbrowder avatar Jul 12 '23 21:07 tbrowder