items = [ "d", "a", "e", "c", "b" ] items.sort #从小到大排序,=> ["a", "b", "c", "d", "e"] items.sort {|x,y| x <=> y } #从小到大排序,=> ["a", "b", "c", "d", "e"] items.sort {|x,y| y <=> x } #从大到小排序, => ["e", "d", "c", "b", "a"]
class Company attr_accessor :nr attr_accessor :name def initialize(nr, name) @nr = nr @name = name endendobjects = [Company.new(2, "d"), Company.new(1, "a"), Company.new(3, "e")]objects.sort {|x,y| x.name <=> y.name } #以模型对象元素name进行模型对象的排序结果:#=> objects = [Company.new(1, "a"), Company.new(2, "d"), Company.new(3, "e")]
【实 × 新 × 奇】http://www.lycom.de/ 最新报道Ruby语言、Rails框架、Ubuntu、MacOSX和软件技术信息
订阅 full-text RSS 或者 comments RSS.
Interesting links can be found at del.icio.us or by subscribing to my del.icio.us feed
Powered by SimpleLog