Sort Question

I don’t understand something (so what else is new) again… lets say I have
a array with some objects in it. the object has stuff in it, thus…

class Area_rep

def initialize (xnum,name)
@xnum = xnum
@name = name
end

attr_accessor :xnum, :name

end

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?

Thanks,

Mark

NOTICE: This e-mail and any attachment(s) may contain confidential and
proprietary information of Goss International Corporation and/or its
subsidiaries and may be legally privileged. This e-mail is intended solely
for the addressee. If you are not the addressee, dissemination, copying or
other use of this e-mail or any of its content is strictly prohibited and
may be unlawful. If you are not the intended recipient please inform the
sender immediately and destroy the e-mail and any copies. All liability for
viruses is excluded to the fullest extent permitted by law. Any views
expressed in this message are those of the individual sender. No contract
may be construed by this e-mail.

I don’t understand something (so what else is new) again… lets say I have
a array with some objects in it. the object has stuff in it, thus…

class Area_rep

def initialize (xnum,name)
@xnum = xnum
@name = name
end

attr_accessor :xnum, :name

end

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?

ary = [Area_rep.new(1,‘a’), Area_rep.new(2,‘b’), Area_rep.new(3,‘c’)]
sortedAry = ary.sort {|a, b| b.xnum <=> a.xnum}

···

On Thursday 05 September 2002 06:58 pm, Firestone, Mark - Technical Support wrote:

Thanks,

Mark

NOTICE: This e-mail and any attachment(s) may contain confidential and
proprietary information of Goss International Corporation and/or its
subsidiaries and may be legally privileged. This e-mail is intended solely
for the addressee. If you are not the addressee, dissemination, copying or
other use of this e-mail or any of its content is strictly prohibited and
may be unlawful. If you are not the intended recipient please inform the
sender immediately and destroy the e-mail and any copies. All liability for
viruses is excluded to the fullest extent permitted by law. Any views
expressed in this message are those of the individual sender. No contract
may be construed by this e-mail.

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.

···

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?


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

Hi,

···

In message “Re: Sort Question” on 02/09/06, Albert Wagner alwagner@tcac.net writes:

I want to sort the array (with these objects in it) by xnum…

ary = [Area_rep.new(1,‘a’), Area_rep.new(2,‘b’), Area_rep.new(3,‘c’)]
sortedAry = ary.sort {|a, b| b.xnum <=> a.xnum}

If you are using 1.7, you can

sortedAry = ary.sort_by {|x| x.xnum}

						matz.