From: Florian Frank Date: 2002-09-06T10:15:43+09:00 Subject: Re: Sort Question Am 2002-09-06 08:58:35 +0900 schrieb Firestone, Mark - Technical Support: > I want to sort the array (with these objects in it) by xnum... > > Or is there a better, more Ruby like way to do things anyhow? Better than what? There are many idiomatic ways to sort an Array in Ruby: class A def initialize(xnum, name) @xnum, @name = xnum, name end attr_accessor :xnum, :name def <=>(other) self.xnum <=> other.xnum end end p ary = ("A".."J").map { |x| A.new(rand(100), x) } p ary.sort { |a, b| a.xnum <=> b.xnum } p ary.sort_by { |x| x.xnum } # Ruby 1.7 only p ary.sort # with A#<=> I don't know which is better for your application. -- The man who has fed the chicken every day throughout its life at last wrings its neck instead, showing that more refined views as to the uniformity of nature would have been useful to the chicken. -- Bertand Russell, "The Problems of Philosophy", 1912