Hash ordering preserved between iterations if not modified?
Prior to 1.9, behavior of enumerated hashes was not in the ruby specification and therefore was up to implementation -- basically, hash enumeration behavior/pattern was undefined by the language and implementations could really do whatever they want (random? sorted? insertion order? different method every time? anything goes!)
1.9+, hash enumeration is specified by the language to be in the order of insertion, so if you know your platform is 1.9+, you can rely on it.
RubySpec
Do dicts preserve iteration order if they are not modified?
Here is what dict.items()
documentation says:
dict.items() return a copy of the dictionary’s list of (key, value) pairs.
If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond.
I think it's reasonable to assume that item ordering won't change if all you do is iteration.
Ruby: What is the order of keys/values returned by Hash.keys and Hash.values methods?
The top of the Ruby 1.9.2 documentation for the Hash class declares:
Hashes enumerate their values in the order that the corresponding keys were inserted.
Cursory tests suggest that this does indeed apply to both Hash#keys
and Hash#values
, although the corresponding documentation for those methods doesn't seem to specify it.
Re-ordering hash items to have some appear at the end
You can try following,
(hash.keys - end_keys + end_keys).map { |key| [key, hash[key]] }.to_h
is the Java HashMap keySet() iteration order consistent?
If it is not stated to be guaranteed in the API documentation, then you shouldn't depend on it. The behavior might even change from one release of the JDK to the next, even from the same vendor's JDK.
You could easily get the set and then just sort it yourself, right?
Occurred order in the iteration at run-time in a map
Hash map document says HashMap makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
that explains though the hashmap is same it can not guaranatee on order. for Ordered map you can use TreeMap or LinkedHashMap
TreeMap API says The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
Is Perl guaranteed to return consistently-ordered hash keys?
Yes. From perldoc -f keys
:
The keys are returned in an apparently random order. The actual random order is subject to change in future versions of perl, but it is guaranteed to be the same order as either the
values
oreach
function produces (given that the hash has not been modified). Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see "Algorithmic Complexity Attacks" inperldoc perlsec
).
(emphasis mine)
Related Topics
Rbenv: Surviving Without Gemsets
What's the Difference Between Process.Fork and Process.Spawn in Ruby 1.9.2
How to Write a Ruby Command Line App That Supports Tab Completion
Find() with Nil When There Are No Records
How to Include Video in Jekyll Markdown Blog
How to Save Values into a Yaml File
Rails. How to Store Time of Day (For Schedule)
How to Call an Older Version of a Gem from the Commandline
Ruby on Rails Advanced JSON Serialization
How to Get Rspec-2 to Give the Full Trace Associated with a Test Failure
How to Use Unicorn as "Rails S"
How to See the Dependency Tree Just from Gemfile
Modify Ruby Hash in Place( Rails Strong Params)
Is There a Less Intrusive Alternative to Rspec's 'Should_Receive'
How to Convert a String to a Class Method
Are the Date, Time, and Datetime Classes Necessary