Reputation: 303
I have a hash
h = {}
h.compare_by_identity
h[2.51] = 1
h1[2.51] = 2
Edit: h1[2.51] = 2
should be h[2.51] = 2
it is ok with duplicate key. But when i use
Hash[h.sort]
it return only one value with key like
{2.51=>2}
is there any way to get the two values from the hash in sorted order?
Upvotes: 2
Views: 75
Reputation: 80065
Since it can't be done with floats, turn them into strings:
h = {}
h.compare_by_identity
h[2.51.to_s] = 2
h[2.51.to_s] = 1
p h.sort # => [["2.51", 1], ["2.51", 2]]
Upvotes: 1
Reputation: 2045
Starting with ruby version 2.0 the key 2.51
is actually the same object (because of ruby internal caching) in both assignments. Try to output 2.51.object_id
for both cases and it will output the same id.
Upvotes: 2