Sort an array alphabetically in Ruby but with capitalised words at the end
来源:互联网 发布:java.util.arraylist 编辑:程序博客网 时间:2024/06/10 10:52
1、["Apple","banana","Zebra","orange"].sort_by(&:swapcase)
Probably not a concern, but
["iPhone", "item"].sort_by(&:swapcase) #=> ["item", "iPhone"]
,["IRB", "It"].sort_by(&:swapcase) #=> ["It", "IRB"]
2、Yet another way:
arr = %w{ Apple banana Zebra Camel iPhone waffles Pancakes } # => ["Apple", "banana", "Zebra", "Camel", "iPhone", "waffles", "Pancakes"] arr.sort.rotate(arr.count { |s| s[/^[A-Z]/] }) # => ["banana", "iPhone", "waffles", "Apple", "Camel", "Pancakes", "Zebra"]
We have:
a = arr.sort # => ["Apple", "Camel", "Pancakes", "Zebra", "banana", "iPhone", "waffles"]nbr_capitalized_words = arr.count { |s| s[/^[A-Z]/] } #=> 4a.rotate(nbr_capitalized_words) #=> ["banana", "iPhone", "waffles", "Apple", "Camel", "Pancakes", "Zebra"]
3、["banana", "Banana","orange", "Apple", ].sort_by{|x| [x[0].upcase==x[0] ? 1:0,x] }
0 0
- Sort an array alphabetically in Ruby but with capitalised words at the end
- Sort an Array Mixed With Integers and Strings - Ruby
- Ruby Arrays: keep repeated values only from the end of an array of arrays
- How to sort an array in Ruby to a particular order?
- Unity 解决 An asset is marked with HideFlags.DontSave but is included in the build 问题。
- An asset is marked with HideFlags.DontSave but is included in the build:
- An asset is marked with HideFlags.DontSave but is included in the build
- An asset is marked with HideFlags.DontSave but is included in the build 解决方法
- Partially sorting an array in Ruby
- Sorting an array of hash in Ruby
- How do I sort an array of hashes by a value in the hash?
- Converting a array to an array of ranges in ruby
- Sort in Array with duplicate keys
- net.sf.json.JSONException: Found starting '{' but missing '}' at the end. at character 0 of null
- begin with the end in mind
- How to format strings in an array in ruby?
- 打包的时候报错 An asset is marked with HideFlags.DontSave but is included in the build的解决办法
- unity 项目build 的时候后报错An asset is marked with HideFlags.DontSave but is included in the build:
- Android ramdisk.img system.img userdata.img介绍与使用
- 服务器监控工具zabbix、nagios、ganglia
- Java图片处理开源框架
- Lisp的前世今生
- hdu2035 人见人爱A^B(快速幂取模)
- Sort an array alphabetically in Ruby but with capitalised words at the end
- Android 编程下通过 Theme 和 Style 避免 APP 启动闪黑屏
- 记天溯实习期间关于mondrian的开发小结
- 微软家族的首个跨平台开发工具 Visual Studio Code
- 分布式系统系列(三)---Web Service实战--CXF理论篇
- 单件模式Singleton Pattern
- android TextView中如何设置下划线
- chromium下载编译过程
- 世界500强面试题(趣味智力测试题)