in reply to Re^7: Keeping Order with YAML::XS (sort keys)
in thread Keeping Order with YAML::XS

... lost ... demonstrating..

that sort was used to sort keys from OPs structure, sorted to BREAK the hard work of Tie::IxHash -- hashes may not have order, but Tie::IxHash keeps order, the YAML modules use "sort keys" to throw away the work of Tie::IxHash , throw away the natural hash order,

  • Comment on Re^8: Keeping Order with YAML::XS (sort keys)

Replies are listed 'Best First'.
Re^9: Keeping Order with YAML::XS (sort keys)
by Loops (Curate) on Jul 29, 2013 at 10:32 UTC

    It was well documented in earlier posts that the ordering of Tie::IxHash was not preserved when serializing to YAML. It would be shocking if any of the CPAN YAML modules had special code to recognize an IxHash. Instead they just see it as a normal hash and proceed properly from there (ie. with freedom to sort keys). The solution has been presented above -- transform the IxHash into an array of hashes each containing a single key/value pair. That is a data structure that will work and answers the question posed by the OP.

    All that remains is for some code to be written.

    Still, a lot of code other than YAML::XS will ignore IxHash ordering...

    use Data::Dump qw(pp); use Tie::IxHash; tie my(%hash), 'Tie::IxHash', (a => 1, c => 3, b => 2); pp \%hash;
    Output:
    { # tied Tie::IxHash a => 1, b => 2, c => 3, }

      Still, a lot of code other than YAML::XS will ignore IxHash ordering..

      Yeah, with sort keys, and thats a bug