Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#1621

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.

Reported by madlep · December 23rd, 2008 @ 02:08 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Pratik
    Pratik
    • State changed from new to wontfix

    Don't think this belongs to core. Should just use OrderedHash.

    Thanks.

    March 7th, 2009 @ 11:08 AM