# Yaml & SortKeys in 1.8.4

**URL:** <https://www.ruby-forum.com/t/yaml-sortkeys-in-1-8-4/71001>\
**Category:** Ruby\
**Created:** [February 20, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/yaml-sortkeys-in-1-8-4/71001 "2006-02-20T21:58:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ron\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@Ron\_M](https://www.ruby-forum.com/u/Ron_M)\
**Post date:** [February 20, 2006, 9:58pm UTC](https://www.ruby-forum.com/t/yaml-sortkeys-in-1-8-4/71001/1 "2006-02-20T21:58:36Z")

</div>

IIRC, in a previous version, I was able to make more  
human-friendly YAML using the :SortKeys option as shown below.

Since upgrading to 1.8.4 the keys seem not to be sorted anymore.  
Has the way of doing this changed? I hope this feature hasn’t  
gone away. The main reason we’re using YAML instead of XML is  
that it was more human-readable, and the SortKeys option added  
a major part of the human-readability.

## irb(main):016:0\> puts( {1,‘a’,2,‘b’,3,‘c’,4,‘d’,5,‘d’}.to\_yaml(:Indent=\>1,:SortKeys=\>true))

5: d  
1: a  
2: b  
3: c  
4: d  
=\> nil

---

<div class="post-metadata">

**Author:** ![Ron\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ce7236/32.png) [@Ron\_M](https://www.ruby-forum.com/u/Ron_M)\
**Post date:** [February 20, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/yaml-sortkeys-in-1-8-4/71001/2 "2006-02-20T22:23:25Z")

</div>

Ron M wrote:

> ## irb(main):016:0\> puts( {1,‘a’,2,‘b’,3,‘c’,4,‘d’,5,‘d’}.to\_yaml(:Indent=\>1,:SortKeys=\>true))
> 
> 5: d  
> 1: a  
> 2: b  
> 3: c  
> 4: d  
> =\> nil

It grieves me also. I submitted a request to:

[http://code.whytheluckystiff.net/syck/ticket/3](http://code.whytheluckystiff.net/syck/ticket/3)

In the interim, you can load the following patch whenever you want to  
get sorted hash keys (for all hashes, unfortunately).

class Hash  
def to\_yaml( opts = {} )  
YAML::quick\_emit( object\_id, opts ) do |out|  
out.map( taguri, to\_yaml\_style ) do |map|  
sorted\_keys = keys  
sorted\_keys = begin  
sorted\_keys.sort  
rescue  
sorted\_keys.sort\_by {|k| k.to\_s} rescue sorted\_keys  
end

```
    sorted_keys.each do |k|
      map.add( k, fetch(k) )
    end
  end
end

```

end  
end
