Sort Question 
Author Message
 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)


 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.



Tue, 22 Feb 2005 07:59:12 GMT  
 Sort Question
On Thursday 05 September 2002 06:58 pm, Firestone, Mark - Technical Support

Quote:
> 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)


>  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}

- Show quoted text -

Quote:

> 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.



Tue, 22 Feb 2005 08:05:24 GMT  
 Sort Question
Am 2002-09-06 08:58:35 +0900 schrieb Firestone, Mark - Technical Support:

Quote:
> 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)

        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



Tue, 22 Feb 2005 09:16:22 GMT  
 Sort Question
Hi,

In message "Re: Sort Question"

|> 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.



Tue, 22 Feb 2005 09:25:01 GMT  
 Sort Question
I like the sort_by syntax.  How stable is 1.7.2?

Mark

[---------]
 "I must hurry back to my comic book store, where I dispense the insults
rather than absorb them."
[---------]

Quote:
-----Original Message-----

Sent: 06 September 2002 02:24

Subject: Re: Sort Question

Hi,

In message "Re: Sort Question"

|> 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.

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.



Tue, 22 Feb 2005 16:33:33 GMT  
 Sort Question

Quote:
----- Original Message -----

> I like the sort_by syntax.  How stable is 1.7.2?

> Mark

I like it too, but can't answer your question.  Bear in mind, however, that you
can add it yourself to 1.6.  I think there's an implementation in Pickaxe.  I
was surprised when I saw Matz's example because I thought the syntax was:

  ary.sort_by :xnum

Gavin



Thu, 24 Feb 2005 01:52:20 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Listbox sorting question

2. Another sorting question

3. CW2003 Browse Sort Question

4. newbie locale/ascii sort question

5. sort question

6. Sort Question

7. Homework: Sorting Question

8. Sort question

9. a sorting question...

10. sorting question

11. list sort question

12. List sorting question

 

 
Powered by phpBB® Forum Software