# What happened to :SortKeys option to #to\_yaml?

**URL:** <https://www.ruby-forum.com/t/what-happened-to-sortkeys-option-to-to-yaml/53396>\
**Category:** Ruby\
**Created:** [February 3, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/what-happened-to-sortkeys-option-to-to-yaml/53396 "2006-02-03T21:06:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 3, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/what-happened-to-sortkeys-option-to-to-yaml/53396/1 "2006-02-03T21:06:45Z")

</div>

I think this used to produce sorted output in 1.8.2:

## irb(main):002:0\> h = {} =\> {} irb(main):003:0\> 10.times {|i| h[i] = i} =\> 10 irb(main):004:0\> puts h.to\_yaml(:SortKeys =\> true)

5: 5  
0: 0  
6: 6  
1: 1  
7: 7  
2: 2  
8: 8  
3: 3  
9: 9  
4: 4  
=\> nil

Is there still a way to do this?

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 3, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/what-happened-to-sortkeys-option-to-to-yaml/53396/2 "2006-02-03T22:51:18Z")

</div>

On Sat, 4 Feb 2006, Joel VanderWerf wrote:

> 0: 0  
> Is there still a way to do this?  
> i just happened to be working on this today

jib:~/eg/ruby/alib/alib-0.3.1 \> cat a.rb  
require “alib”

oh = ALib::OrderedHash::new

(“a”…“z”).to\_a.reverse.each{|x| oh[x] = x}

oh.yaml\_inline!

y oh

jib:~/eg/ruby/alib/alib-0.3.1 \> RUBYLIB=./lib ruby a.rb  
— { z: z, y: y, x: x, w: w, v: v, u: u, t: t, s: s, r: r, q: q, p:  
p, o: o, n: n, m: m, l: l, k: k, j: j, i: i, h: h, g: g, f: f, e: e, d:  
d, c: c, b: b, a: a }

the class is in alib now, but the yaml\_inline will break on 1.8.4.  
releasing soon.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 3, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/what-happened-to-sortkeys-option-to-to-yaml/53396/3 "2006-02-03T23:09:33Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > * * *
> > 
> > =\> nil  
> > (“a”…“z”).to\_a.reverse.each{|x| oh[x] = x}
> 
> the class is in alib now, but the yaml\_inline will break on 1.8.4.  
> releasing soon.
> 
> cheers.
> 
> -a

Not quite the same, is it?

I don’t want to preserve the order in which I add keys. I just want the  
YAML output to be sorted by key.

Also, I want the old behavior of :SortKeys, which was to do this  
recursively to all hashes hanging off the object you call #to\_yaml on.

This is mostly for aesthetic reasons (and being able to find keys in the  
output doc).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [February 3, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/what-happened-to-sortkeys-option-to-to-yaml/53396/4 "2006-02-03T23:21:45Z")

</div>

On Fri, 3 Feb 2006, Joel VanderWerf wrote:

> Not quite the same, is it?

you’re right.

> I don’t want to preserve the order in which I add keys. I just want the YAML  
> output to be sorted by key.
> 
> Also, I want the old behavior of :SortKeys, which was to do this recursively  
> to all hashes hanging off the object you call #to\_yaml on.
> 
> This is mostly for aesthetic reasons (and being able to find keys in the  
> output doc).

hmmm. ifirc yaml always does this for hashes now… i may be mistaken  
though.

i actually hit that same bug today!

-a
