certik / yaml-cpp

Automatically exported from code.google.com/p/yaml-cpp
MIT License
0 stars 0 forks source link

Ordering of map elements not retained #187

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi and thank you for this very nice and useful library! 

I noticed that when reading maps and iterating over them, the ordering seems to 
be inconsistent sometimes. Is this intentional? I'd rather be able to rely on 
the iterator providing the elements in the order they are defined in the YAML 
source, or at least in some ordering that makes sense (alphabetically?). 

Example: 
node = YAML::Load("{foo: 1, bar: two, baz: 7.56, Test String: test_string with 
spaces}");
for(YAML::Node::iterator it = node.begin(); it != node.end(); ++it)
{
    std::string key = it->first.as<std::string>();
    std::string value = it->second.as<std::string>();
    cerr<<key<<": "<<value<<endl;
}

gives the output: 
baz: 7.56
bar: two
Test String: test_string with spaces
foo: 1

What version of the product are you using? On what operating system?

Version 0.5.0, Sabayon Linux (Gentoo)

Please provide any additional information below.

Might be related to Issue 169

Original issue reported on code.google.com by oster.t...@gmail.com on 29 Jan 2013 at 1:35

GoogleCodeExporter commented 9 years ago
You're right, this is a dupe of Issue 169.

This is sorta intentional. YAML doesn't specify the ordering of key/value pairs 
in a map, so you're not supposed to rely on it. If you want an ordered map, the 
suggested way to do it is:

- baz: 7.56
- bar: two
- Test String: test_string with spaces
- foo: 1

However, I do see some value to it, so I will consider it. I'm just not 
convinced yet :)

Original comment by jbe...@gmail.com on 29 Jan 2013 at 2:22