This project is archived and is in readonly mode.

#1621 ✓wontfix
madlep

Add #each_sorted method to Hash to allow easier iteration of sorted key/value pairs

Reported by madlep | December 23rd, 2008 @ 02:08 PM | in 2.x

Added #each_sorted method to Hash. Allows sorted iteration identical to #each (except the keys are sorted).

e.g.


original = {'c' => 3, 'a' => 1, 'b' => 2, 'd' => 4}
expected = [['a',1],['b',2],['c',3],['d',4]]
original.each_sorted do |key, value|
  # key will be a,b,c,d etc on each iteration in order
  assert_equal expected.shift, [key, value]
end

Made change after watching http://railscasts.com/episodes/2... and realizing I'd had the same problem myself a few times.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages