people = {:fred => 23,:joan => 18,:pete => 54
}

年龄排序:

people.values.sort    # => [18, 23, 54]

姓名排序:

people.sort_by { |name, age| age }# => [[:joan, 18], [:fred, 23], [:pete, 54]]

或者

people.sort

一个复杂点的hash

people = {:fred => { :name => "Fred", :age => 23 },:joan => { :name => "Joan", :age => 18 },:pete => { :name => "Pete", :age => 54 }
}

people.sort_by { |k, v| v[:age] }# => [[:joan, {:name=>"Joan", :age=>18}], [:fred, {:name=>"Fred", :age=>23}],[:pete, {:name=>"Pete", :age=>54}]]

Ruby:Hash 排序相关推荐

  1. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  2. ruby hash方法_Ruby中带有示例的Hash.select方法

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

  3. ruby hash方法_Ruby中带有示例的Hash.default(key = nil)方法

    ruby hash方法 Hash.default(key = nil)方法 (Hash.default(key=nil) Method) In this article, we will study ...

  4. ruby hash方法_Ruby中带有示例的Hash.length方法

    ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...

  5. ruby hash方法_Ruby中带有示例的Hash.flatten方法

    ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...

  6. ruby hash方法_Ruby中带有示例的Hash.key?(obj)方法

    ruby hash方法 Hash.key?(obj)方法 (Hash.key?(obj) Method) In this article, we will study about Hash.key?( ...

  7. ruby hash方法_Ruby中带有示例的Hash.rassoc(obj)方法

    ruby hash方法 Hash.rassoc(obj)方法 (Hash.rassoc(obj) Method) In this article, we will study about Hash.r ...

  8. ruby hash方法_Ruby中带有示例的Hash.keys方法

    ruby hash方法 哈希键方法 (Hash.keys Method) In this article, we will study about Hash.keys Method. The work ...

  9. ruby hash方法_Hash.fetch()方法以及Ruby中的示例

    ruby hash方法 Hash.fetch()方法 (Hash.fetch() Method) In this article, we will study about Hash.fetch() M ...

  10. ruby hash方法_Ruby中带有示例的Hash.rehash方法

    ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...

最新文章

  1. 方舟服务器显示队友位置,方舟如何看队友在哪 | 手游网游页游攻略大全
  2. Redis数据库的初步认识(二)-C/C++连接redis数据库
  3. 【短文本聚类】TextCluster:短文本聚类预处理模块 Short text cluster
  4. 什么是SMART原则?
  5. postgresql 客户端登录配置
  6. 谎言,统计数据和供应商
  7. js实现数组翻转(倒序输出)
  8. 城市地铁类毕业论文文献包含哪些?
  9. 不记得是哪部电影里的段子了
  10. 3 天天向上的力量 c语言
  11. VR游戏开发干货教程:如何创建一个VR项目
  12. 如何写出令人惊叹的设计文档?
  13. 最好的跑步耳机推荐、盘点五款公认最好的跑步耳机
  14. supervisor reload报错及恢复方法
  15. projectManager
  16. cesium 街景 vs 三维视图 显卡 知识点.txt
  17. mac的照片应用照片原图存储位置
  18. 容器云平台使用体验:DaoCloud
  19. redis命令配置别名步骤
  20. 新任技术管理者应该怎样去开始帮助他人?

热门文章

  1. 全球及中国碳纤维风电叶片市场投资分析与运营潜力预测报告2021-2027年
  2. 中国环境监测行业现状研究与投资规划分析报告2022-2028年
  3. 全球及中国铝行业产销需求与未来前景预测分析报告2022-2028年版
  4. Application Desktop Toolbars 桌面工具栏
  5. tensorflow基础
  6. xtraback工具的使用和备份
  7. erlang mnesia数据库简单应用
  8. Android 之窗口小部件高级篇--App Widget 之 RemoteViews - 跨到对岸去
  9. [转] iOS中@class #import #include 简介
  10. 我们为什么要把方法区分为:静态方法和实例化方法 ?(摘)