[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to sort array ascending, except zero ?

Paganoni

6/8/2009 8:39:00 AM

Hello, I need to sort
[1,4,2,0,8,9] to [1,2,4,8,9,0]

A simple ascending sort but with the zero values to the end

I really don't see how to do

Thanks for your help
23 Answers

Andrew Timberlake

6/8/2009 8:49:00 AM

0

On Mon, Jun 8, 2009 at 10:40 AM, Paganoni<noway@fakenullvoid.com> wrote:
> Hello, I need to sort
> [1,4,2,0,8,9] to [1,2,4,8,9,0]
>
> A simple ascending sort but with the zero values to the end
>
> I really don't see how to do
>
> Thanks for your help
>

Maybe not the most elegant solution but it gets the job done:
arr = [1,4,2,0,8,9]
arr = (arr - [0]).sort << 0

Otherwise you want to override <=> on Fixnum

Andrew Timberlake
http://ramblingso...

http://MyM... - The SIMPLE way to manage your savings

Martin DeMello

6/8/2009 9:03:00 AM

0

On Mon, Jun 8, 2009 at 2:10 PM, Paganoni<noway@fakenullvoid.com> wrote:
> Hello, I need to sort
> [1,4,2,0,8,9] to [1,2,4,8,9,0]
>
> A simple ascending sort but with the zero values to the end

max = a.max + 1
# or max = 2 ** 31, say, if you don't want the extra pass
# but sorting is O(n log n) and max is O(n) so it doesn't really matter

a.sort_by {|x| x.zero? ? max : x}

martin

Robert Klemme

6/8/2009 9:21:00 AM

0

2009/6/8 Andrew Timberlake <andrew@andrewtimberlake.com>:
> On Mon, Jun 8, 2009 at 10:40 AM, Paganoni<noway@fakenullvoid.com> wrote:
>> Hello, I need to sort
>> [1,4,2,0,8,9] to [1,2,4,8,9,0]
>>
>> A simple ascending sort but with the zero values to the end

> Otherwise you want to override <=> on Fixnum

Definitively not! Changing the default Fixnum ordering is dangerous
because it will likely break a lot of other code and it is
superfluous, too. There are better tools for that, i.e. defining the
sort order in the place where it is needed (see Martin's solution).

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Robert Klemme

6/8/2009 9:28:00 AM

0

2009/6/8 Martin DeMello <martindemello@gmail.com>:
> On Mon, Jun 8, 2009 at 2:10 PM, Paganoni<noway@fakenullvoid.com> wrote:
>> Hello, I need to sort
>> [1,4,2,0,8,9] to [1,2,4,8,9,0]
>>
>> A simple ascending sort but with the zero values to the end
>
> max = a.max + 1
> # or max = 2 ** 31, say, if you don't want the extra pass
> # but sorting is O(n log n) and max is O(n) so it doesn't really matter

Still traversing the array twice just to get the max beforehand does
not /feel/ right. I'd rather use your "large constant" - maybe even
with a _really_ large number: :-)

irb(main):020:0> a = [1,4,2,0,8,9]
=> [1, 4, 2, 0, 8, 9]
irb(main):021:0> INF = 1.0 / 0.0
=> Infinity
irb(main):022:0> a.sort_by {|x| x == 0 ? INF : x}
=> [1, 2, 4, 8, 9, 0]

Another good alternative is to use the block form of #sort:

irb(main):023:0> a.sort do |x,y|
irb(main):024:1* case
irb(main):025:2* when x == 0 then 1
irb(main):026:2> when y == 0 then -1
irb(main):027:2> else x <=> y
irb(main):028:2> end
irb(main):029:1> end
=> [1, 2, 4, 8, 9, 0]

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Martin DeMello

6/8/2009 9:33:00 AM

0

On Mon, Jun 8, 2009 at 2:57 PM, Robert Klemme<shortcutter@googlemail.com> w=
rote:
> Still traversing the array twice just to get the max beforehand does
> not /feel/ right. =A0I'd rather use your "large constant" - maybe even
> with a _really_ large number: :-)
>
> irb(main):020:0> a =3D [1,4,2,0,8,9]
> =3D> [1, 4, 2, 0, 8, 9]
> irb(main):021:0> INF =3D 1.0 / 0.0
> =3D> Infinity

Ah yes, forgot you could compare floats with fixnums :)

martin

Bertram Scharpf

6/8/2009 10:32:00 AM

0

Hi,

Am Montag, 08. Jun 2009, 17:40:06 +0900 schrieb Paganoni:
> Hello, I need to sort
> [1,4,2,0,8,9] to [1,2,4,8,9,0]
> A simple ascending sort but with the zero values to the end

I don't know what you want to do further with the sorted values
but maybe this is an approach to consider:

s = a.map { |x| x.nonzero? }
s.compact!
s.sort!

or

s, z = a.map { |x| x.nonzero? }.partition { |x| x }
s.sort!
puts z.length

Bertram


P.S.: Still, my opinion is that there should be an equivalent
String#notempty? to Numeric#nonzero? !

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Harry Kakueki

6/8/2009 11:00:00 AM

0

On Mon, Jun 8, 2009 at 5:40 PM, Paganoni<noway@fakenullvoid.com> wrote:
> Hello, I need to sort
> [1,4,2,0,8,9] to [1,2,4,8,9,0]
>
> A simple ascending sort but with the zero values to the end
>
> I really don't see how to do
>
> Thanks for your help
>
>



tmp = arr.partition{|x| x != 0 }
(tmp[0].sort + tmp[1])

Harry
--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Harry Kakueki

6/8/2009 11:08:00 AM

0

On Mon, Jun 8, 2009 at 7:59 PM, Harry Kakueki<list.push@gmail.com> wrote:
> On Mon, Jun 8, 2009 at 5:40 PM, Paganoni<noway@fakenullvoid.com> wrote:
>> Hello, I need to sort
>> [1,4,2,0,8,9] to [1,2,4,8,9,0]
>>
>> A simple ascending sort but with the zero values to the end
>>
>> I really don't see how to do
>>
>> Thanks for your help
>>
>>
>
>
>
> tmp = arr.partition{|x| x != 0 }
> (tmp[0].sort + tmp[1])
>
> Harry
> --
> A Look into Japanese Ruby List in English
> http://www.kakueki.com/ruby...
>

Oops!

tmp = arr.partition{|x| x != 0 }
p (tmp[0].sort + tmp[1])

The second line should be like this, of course.

Harry


--
A Look into Japanese Ruby List in English
http://www.kakueki.com/ruby...

Paganoni

6/8/2009 1:10:00 PM

0

le 08/06/2009 10:38, Paganoni nous a dit:
> Hello, I need to sort
> [1,4,2,0,8,9] to [1,2,4,8,9,0]
>
> A simple ascending sort but with the zero values to the end
>
> I really don't see how to do
>
> Thanks for your help

I forgot to mention that the array can contain several 0 values not only
one.

Giampiero Zanchi

6/8/2009 1:44:00 PM

0

ciao
if you are sure not to have negative numbers in your input, then you can
map each number to its negative, sort descending and then map again to
positive
--
Posted via http://www.ruby-....