Array Problem, sort Array

[code]
Array = ["a", "a", "a", "b", "b", “c”, "c", "c","d", "d", "e"]
[/code]

How can i sort the array data???

[result]
Array = ["a", "b", "c", "d", "e"]
[/result]

···

--
Posted via http://www.ruby-forum.com/.

["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"].uniq.sort

array = ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"].uniq.sort

puts array.join("WOOT!\n")

···

On 6/27/07, Cool Wong <coolwong85@yahoo.com> wrote:

[code]
Array = ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"]
[/code]

How can i sort the array data???

[result]
Array = ["a", "b", "c", "d", "e"]
[/result]

--
Posted via http://www.ruby-forum.com/\.

[code]
Array = ["a", "a", "a", "b", "b", “c”, "c", "c","d", "d", "e"]
[/code]

Can i calculate the data in the Array???

a a a b b c c c d d e

For example: ArrayNumber = ["3", "2", "3", "2", "1"]

···

--
Posted via http://www.ruby-forum.com/.

List Rb wrote:

["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"].uniq.sort

array = ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"].uniq.sort

puts array.join("WOOT!\n")

if the array include the "nil", it cannot sort, why??

["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e", nil].uniq.sort

···

--
Posted via http://www.ruby-forum.com/\.

["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e",
nil].compact.uniq.sort

···

On 6/27/07, Cool Wong <coolwong85@yahoo.com> wrote:

List Rb wrote:
> ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"].uniq.sort
>
> array = ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"].uniq.sort
>
> puts array.join("WOOT!\n")

if the array include the "nil", it cannot sort, why??

["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e", nil].uniq.sort

--
Posted via http://www.ruby-forum.com/\.

On Behalf Of Cool Wong:
# if the array include the "nil", it cannot sort, why??
# ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e", nil].uniq.sort

mainly because

1 sort expects same class for comparison
  irb(main):011:0> [1,"a"].sort
  ArgumentError: comparison of Fixnum with String failed
     from (irb):11:in `sort'
  
2 nil does not have comparison method <=>
  irb(main):012:0> [nil,nil].sort
  NoMethodError: undefined method `<=>' for nil:NilClass
     from (irb):12:in `sort'

solution: a) convert elements to some common class (string is common)
          b) filter those you don't want sorted (using compact/reject/..)
             or select those you want (using select/map/..)

A little digression for everyone though...
  irb(main):013:0> [nil].sort
  => [nil]

is that intended?

kind regards -botp

my_array.compact.uniq.sort

···

On Jun 27, 9:39 pm, Cool Wong <coolwon...@yahoo.com> wrote:

if the array include the "nil", it cannot sort, why??

["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e", nil].uniq.sort

w/ numbers also in case that's your next question :slight_smile:

[1,2,4, nil,3,"a"].compact.collect {|i| i.to_s}.sort.uniq

You should check out http://dev.rubycentral.com/ref/ref_c_array.html#sort

···

On 6/27/07, list. rb <list.rb@gmail.com> wrote:

["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e",
nil].compact.uniq.sort

On 6/27/07, Cool Wong <coolwong85@yahoo.com> wrote:
>
> List Rb wrote:
> > ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e"].uniq.sort
> >
> > array = ["a", "a", "a", "b", "b", "c", "c", "c","d", "d",
"e"].uniq.sort
> >
> > puts array.join("WOOT!\n")
>
> if the array include the "nil", it cannot sort, why??
>
> ["a", "a", "a", "b", "b", "c", "c", "c","d", "d", "e", nil].uniq.sort
>
> --
> Posted via http://www.ruby-forum.com/\.
>

...why wouldn't it be? Sorting an array of any 1 element should always
return a similar array.

···

On Jun 27, 10:17 pm, Peña, Botp <b...@delmonte-phil.com> wrote:

A little digression for everyone though...
  irb(main):013:0> [nil].sort
  => [nil]

is that intended?

solution: a) convert elements to some common class (string is common)
          b) filter those you don't want sorted (using compact/reject/..)
             or select those you want (using select/map/..)

c) Use a sort(_by) block (maybe that's what you meant in a) :

["a", nil, "b"].sort_by { |e| (e.nil? ? 'z' : e) }

=> ["a", "b", nil]

A little digression for everyone though...
  irb(main):013:0> [nil].sort
  => [nil]

is that intended?

Well, you don't need the comparison operator where there's only one
element, do you ?

Fred

···

Le 28 juin à 06:17, Peña, Botp a écrit :
--
There is no god up in the sky tonight No sign of heaven anywhere in
sight All that was true is left behind Once I could see now I am blind
Don't want the dreams you try to sell This disease I give to myself
                                                 (Nine Inch Nails, Suck)

# > A little digression for everyone though...
# > irb(main):013:0> [nil].sort
# > => [nil]
# >
# > is that intended?

···

From: Phrogz [mailto:gavin@refinery.com] :
# On Jun 27, 10:17 pm, Peña, Botp <b...@delmonte-phil.com> wrote:
#
# ...why wouldn't it be? Sorting an array of any 1 element should always
# return a similar array.

it gives the impression that nil elements are sortable.

kind regards -botp

# Le 28 juin à 06:17, Peña, Botp a écrit :
# > solution: a) convert elements to some common class (string
# is common)
# > b) filter those you don't want sorted (using
# compact/reject/..)
# > or select those you want (using select/map/..)

···

From: F. Senault [mailto:fred@lacave.net] :
#
# c) Use a sort(_by) block (maybe that's what you meant in a) :
# >> ["a", nil, "b"].sort_by { |e| (e.nil? ? 'z' : e) }
# => ["a", "b", nil]

yap, they're all the same, string comparison.

another stupid sample below. the comparison is in integers, indirectly handled by array#index.

shelf_order = [nil, "orange","spices","apple","peaches", "herbs"]
tobe_ordered = ["herbs", "orange","apple",nil,"peaches"]
tobe_ordered.sort_by do |e|
   shelf_order.index(e)
end
=> [nil, "orange", "apple", "peaches", "herbs"]

# > A little digression for everyone though...
# > irb(main):013:0> [nil].sort
# > => [nil]
# >
# > is that intended?
#
# Well, you don't need the comparison operator where there's only one
# element, do you ?

careful with the "one" there. The array returned may be complex.
eg,
irb(main):029:0> [[nil,[nil,"a"]]].sort
=> [[nil, [nil, "a"]]]
irb(main):030:0> [[nil,[nil,"a"]]].flatten.sort
NoMethodError: undefined method `<=>' for nil:NilClass
        from (irb):30:in `sort'

again, as i've said to Phrogz, the behavior hints that nil elements by themselves are sortable. Remember, programs contain blackboxes wc may be complex and not obvious, eg
   complex_proc_ret_array.sort_complex.foo_proc.bar_proc

one could argue bluntly too, eg
[nil].sort
=>[nil] # yes, there is no need to sort
[nil,nil].sort
=>[nil,nil] # yes, there is no need to sort

I really value consistency of object behavior when it comes to ruby, maybe because it's too dynamic and i want least number of surprises (especially when you chain things).

Anyway, I've overlooked this behavior and now I'll have to recheck again my testcases :wink:

thanks Senault and Phrogz, and kind regards -botp

Let's do some philosophy!
But if an array has only one element and the element is nil, is it really an array?
It is an object of Class Array.
Well, it must be an array.

···

On Jun 28, 2007, at 2:41 AM, Peña, Botp wrote:

From: Phrogz [mailto:gavin@refinery.com] :
# On Jun 27, 10:17 pm, Peña, Botp <b...@delmonte-phil.com> wrote:
# > A little digression for everyone though...
# > irb(main):013:0> [nil].sort
# > => [nil]
# >
# > is that intended?
#
# ...why wouldn't it be? Sorting an array of any 1 element should always
# return a similar array.

it gives the impression that nil elements are sortable.

kind regards -botp

Hi --

···

On Thu, 28 Jun 2007, Peña, Botp wrote:

# > A little digression for everyone though...
# > irb(main):013:0> [nil].sort
# > => [nil]
# >
# > is that intended?
#
# Well, you don't need the comparison operator where there's only one
# element, do you ?

careful with the "one" there. The array returned may be complex.
eg,
irb(main):029:0> [[nil,[nil,"a"]]].sort
=> [[nil, [nil, "a"]]]
irb(main):030:0> [[nil,[nil,"a"]]].flatten.sort
NoMethodError: undefined method `<=>' for nil:NilClass
       from (irb):30:in `sort'

again, as i've said to Phrogz, the behavior hints that nil elements by themselves are sortable. Remember, programs contain blackboxes wc may be complex and not obvious, eg
  complex_proc_ret_array.sort_complex.foo_proc.bar_proc

one could argue bluntly too, eg
[nil].sort
=>[nil] # yes, there is no need to sort
[nil,nil].sort
=>[nil,nil] # yes, there is no need to sort

I really value consistency of object behavior when it comes to ruby, maybe because it's too dynamic and i want least number of surprises (especially when you chain things).

What about:

   class C; end
   [C.new].sort

?

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

# Well, you don't need the comparison operator where there's only one
# element, do you ?

careful with the "one" there. The array returned may be complex.

We're in an object paradigm here. We're trying to sort an array of one
and only one object ; the nature of this object doesn't enter into the
equation. You just don't need to perform any comparisons to sort an
array of one element... or zero :

.sort

=>

Fred

···

Le 28 juin à 12:43, Peña, Botp a écrit :
--
Au bout De la course Remonte jusqu'a A la source One trip One noise
Circuit Nuit bleue Spécialiste De l'enjeu One trip One noise
Longue attente avant de s'elancer One trip (Noir Désir,
Longue vie et tout a recracher One noise One Trip / One Noise)

This thread continues to remind me of a comment made by a colleague in about 1988:

"When searching the linked-list, the algorithm stops at the
  first match unless the list has only one element and then
  it goes to the end whether or not a match is found."

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Jun 28, 2007, at 9:53 AM, John Joyce wrote:

On Jun 28, 2007, at 2:41 AM, Peña, Botp wrote:

From: Phrogz [mailto:gavin@refinery.com] :
# On Jun 27, 10:17 pm, Peña, Botp <b...@delmonte-phil.com> wrote:
# > irb(main):013:0> [nil].sort
# > => [nil]

it gives the impression that nil elements are sortable.

Let's do some philosophy!
But if an array has only one element and the element is nil, is it really an array?
It is an object of Class Array.
Well, it must be an array.

Of course it's an Array. And (unlike Lua) it is an Array with exactly
1 object in it. The fact that that object happens to be a member of
NilClass is no different than objects of TrueClass or Fixnum or yet
another Array.

[nil] !=
[1,nil] != [1]
[1,nil,2] != [1,2]

It's more than the fact that Ruby thinks they're different. They have
completely different, distinct, well-defined meanings.

···

On Jun 28, 7:53 am, John Joyce <dangerwillrobinsondan...@gmail.com> wrote:

> # On Jun 27, 10:17 pm, Peña, Botp <b...@delmonte-phil.com> wrote:
> # > irb(main):013:0> [nil].sort
> # > => [nil]

Let's do some philosophy!
But if an array has only one element and the element is nil, is it
really an array?
It is an object of Class Array.
Well, it must be an array.

When i found a shoppyng under ruby?

···

2007/6/28, dblack@wobblini.net <dblack@wobblini.net>:

Hi --

On Thu, 28 Jun 2007, Peña, Botp wrote:

> # > A little digression for everyone though...
> # > irb(main):013:0> [nil].sort
> # > => [nil]
> # >
> # > is that intended?
> #
> # Well, you don't need the comparison operator where there's only one
> # element, do you ?
>
> careful with the "one" there. The array returned may be complex.
> eg,
> irb(main):029:0> [[nil,[nil,"a"]]].sort
> => [[nil, [nil, "a"]]]
> irb(main):030:0> [[nil,[nil,"a"]]].flatten.sort
> NoMethodError: undefined method `<=>' for nil:NilClass
> from (irb):30:in `sort'
>
> again, as i've said to Phrogz, the behavior hints that nil elements by
themselves are sortable. Remember, programs contain blackboxes wc may be
complex and not obvious, eg
> complex_proc_ret_array.sort_complex.foo_proc.bar_proc
>
> one could argue bluntly too, eg
> [nil].sort
> =>[nil] # yes, there is no need to sort
> [nil,nil].sort
> =>[nil,nil] # yes, there is no need to sort
>
> I really value consistency of object behavior when it comes to ruby,
maybe because it's too dynamic and i want least number of surprises
(especially when you chain things).

What about:

  class C; end
  [C.new].sort

?

David

--
* Books:
  RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
  RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
    & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

--
Juan Pedro Meriño Diaz
Ing de Sistema CURN

# On Jun 28, 2007, at 2:41 AM, Peña, Botp wrote:
# > From: Phrogz [mailto:gavin@refinery.com] :

···

From: John Joyce [mailto:dangerwillrobinsondanger@gmail.com] :
# > # On Jun 27, 10:17 pm, Peña, Botp <b...@delmonte-phil.com> wrote:
# > # > A little digression for everyone though...
# > # > irb(main):013:0> [nil].sort
# > # > => [nil]
# > # > is that intended?
# > # ...why wouldn't it be? Sorting an array of any 1 element should
# > always
# > # return a similar array.
# > it gives the impression that nil elements are sortable.
# Let's do some philosophy!
# But if an array has only one element and the element is nil, is it
# really an array?
# It is an object of Class Array.
# Well, it must be an array.

Yes, Joyce, that is the point. simple as it is.
if [nil].sort is an array, (indeed it is)
isn't [nil,nil].sort an array, too?

kind regards -botp

On Behalf Of dblack@wobblini.net:
# hat about:
# class C; end
# [C.new].sort
# ?

or maybe,
[complex_objects].sort

i really avoid threading such complex objects to sort, though things like that can be handled by sort_by or plain sort{0}.

What i'm concerned at is nil, since it's one of the most common object returned.

But i think i see the crux here, eg

irb(main):042:0> 1 === 1
=> true
irb(main):043:0> 1 == 1
=> true
irb(main):044:0> 1 > 1
=> false
irb(main):045:0> 1 < 1
=> false
irb(main):046:0> nil === nil
=> true
irb(main):047:0> nil == nil
=> true
irb(main):048:0> nil > nil
NoMethodError: undefined method `>' for nil:NilClass
        from (irb):48

···

from :0
irb(main):049:0> nil < nil
NoMethodError: undefined method `<' for nil:NilClass
        from (irb):49
        from :0

?
kind regards -botp