---
title: "Hash自然遍历问题"
author: "axgle"
date: "2007-04-11T16:34:10Z"
tags: []
slug: "hash"
draft: false
lastmod: "2007-04-11T16:34:10Z"
id: 70055
url: "https://axgle.iteye.com/blog/70055"
---

```
h={
    1=>"a",
    21=>"b－1",
    22=>"b－2",
    23=>"b－3",
    3=>"c",
    4=>"d"
}

h.each do |k,v|
   puts k.to_s + "=>" + v
end
```


结果为：

```
22=>b－2
23=>b－3
1=>a
3=>c
4=>d
21=>b－1
```


我期望的结果应为:


```
1=>a
21=>b－1
22=>b－2
23=>b－3
3=>c
4=>d
```


一般的问题:Hash里每个元素是按照什么顺序建立的，那么就按照什么顺序遍历.ruby如何处理这个问题?