Array Problem, sort Array

Discussion in 'Ruby' started by Cool Wong, Jun 28, 2007.

  1. Cool Wong

    Cool Wong Guest

    Code:
    Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]
    
    How can i sort the array data???

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

    --
    Posted via http://www.ruby-forum.com/.
    Cool Wong, Jun 28, 2007
    #1
    1. Advertising

  2. Cool Wong

    Cool Wong Guest

    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/.
    Cool Wong, Jun 28, 2007
    #2
    1. Advertising

  3. Cool Wong

    Phrogz Guest

    On Jun 27, 9:39 pm, Cool Wong <> 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


    my_array.compact.uniq.sort
    Phrogz, Jun 28, 2007
    #3
  4. Cool Wong

    Phrogz Guest

    On Jun 27, 10:17 pm, Peña, Botp <> 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.
    Phrogz, Jun 28, 2007
    #4
  5. Cool Wong

    Peña, Botp Guest

    From: Phrogz [mailto:] :
    # On Jun 27, 10:17 pm, Pe=F1a, Botp <> wrote:
    # > A little digression for everyone though...
    # > irb(main):013:0> [nil].sort
    # > =3D> [nil]
    # >
    # > is that intended?
    #=20
    # ...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
    Peña, Botp, Jun 28, 2007
    #5
  6. Cool Wong

    Cool Wong Guest

    Code:
    Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]
    

    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/.
    Cool Wong, Jun 28, 2007
    #6
  7. Cool Wong

    F. Senault Guest

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


    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
    --
    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)
    F. Senault, Jun 28, 2007
    #7
  8. Cool Wong

    Peña, Botp Guest

    From: F. Senault [mailto:] :
    # Le 28 juin =E0 06:17, Pe=F1a, Botp a =E9crit :
    # > solution: a) convert elements to some common class (string=20
    # is common)
    # > b) filter those you don't want sorted (using=20
    # compact/reject/..)
    # > or select those you want (using select/map/..)
    #=20
    # c) Use a sort(_by) block (maybe that's what you meant in a) :
    # >> ["a", nil, "b"].sort_by { |e| (e.nil? ? 'z' : e) }
    # =3D> ["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 =3D [nil, "orange","spices","apple","peaches", "herbs"]
    tobe_ordered =3D ["herbs", "orange","apple",nil,"peaches"]
    tobe_ordered.sort_by do |e|=20
    shelf_order.index(e)
    end
    =3D> [nil, "orange", "apple", "peaches", "herbs"]

    =20
    # > A little digression for everyone though...
    # > irb(main):013:0> [nil].sort
    # > =3D> [nil]
    # >
    # > is that intended?
    #=20
    # 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
    =3D> [[nil, [nil, "a"]]]
    irb(main):030:0> [[nil,[nil,"a"]]].flatten.sort
    NoMethodError: undefined method `<=3D>' 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
    =3D>[nil] # yes, there is no need to sort
    [nil,nil].sort
    =3D>[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 ;)

    thanks Senault and Phrogz, and kind regards -botp
    Peña, Botp, Jun 28, 2007
    #8
  9. Cool Wong

    John Joyce Guest

    On Jun 28, 2007, at 2:41 AM, Pe=F1a, Botp wrote:

    > From: Phrogz [mailto:] :
    > # On Jun 27, 10:17 pm, Pe=F1a, Botp <> wrote:
    > # > A little digression for everyone though...
    > # > irb(main):013:0> [nil].sort
    > # > =3D> [nil]
    > # >
    > # > is that intended?
    > #
    > # ...why wouldn't it be? Sorting an array of any 1 element should =20
    > always
    > # return a similar array.
    >
    > it gives the impression that nil elements are sortable.
    >
    > kind regards -botp
    >

    Let's do some philosophy!
    But if an array has only one element and the element is nil, is it =20
    really an array?
    It is an object of Class Array.
    Well, it must be an array.=
    John Joyce, Jun 28, 2007
    #9
  10. On Jun 28, 2007, at 9:53 AM, John Joyce wrote:
    > On Jun 28, 2007, at 2:41 AM, Pe=F1a, Botp wrote:
    >> From: Phrogz [mailto:] :
    >> # On Jun 27, 10:17 pm, Pe=F1a, Botp <> wrote:
    >> # > irb(main):013:0> [nil].sort
    >> # > =3D> [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 =20
    > really an array?
    > It is an object of Class Array.
    > Well, it must be an array.


    This thread continues to remind me of a comment made by a colleague =20
    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 Biedenharn, Jun 28, 2007
    #10
  11. Cool Wong

    Phrogz Guest

    On Jun 28, 7:53 am, John Joyce <>
    wrote:
    > > # On Jun 27, 10:17 pm, Peña, Botp <> 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.


    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.
    Phrogz, Jun 28, 2007
    #11
  12. Cool Wong

    Guest

    ---2049402039-258736838-1183046258=:23351
    Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-258736838-1183046258=:23351"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    ---2049402039-258736838-1183046258=:23351
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    Hi --

    On Thu, 28 Jun 2007, Pe=F1a, Botp wrote:

    > # > A little digression for everyone though...
    > # > irb(main):013:0> [nil].sort
    > # > =3D> [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
    > =3D> [[nil, [nil, "a"]]]
    > irb(main):030:0> [[nil,[nil,"a"]]].flatten.sort
    > NoMethodError: undefined method `<=3D>' for nil:NilClass
    > from (irb):30:in `sort'
    >
    > again, as i've said to Phrogz, the behavior hints that nil elements by th=

    emselves are sortable. Remember, programs contain blackboxes wc may be comp=
    lex and not obvious, eg
    > complex_proc_ret_array.sort_complex.foo_proc.bar_proc
    >
    > one could argue bluntly too, eg
    > [nil].sort
    > =3D>[nil] # yes, there is no need to sort
    > [nil,nil].sort
    > =3D>[nil,nil] # yes, there is no need to sort
    >
    > I really value consistency of object behavior when it comes to ruby, mayb=

    e 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

    --=20
    * 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)
    ---2049402039-258736838-1183046258=:23351--
    ---2049402039-258736838-1183046258=:23351--
    , Jun 28, 2007
    #12
  13. Cool Wong

    Peña, Botp Guest

    From: John Joyce [mailto:] :
    # On Jun 28, 2007, at 2:41 AM, Pe=F1a, Botp wrote:
    # > From: Phrogz [mailto:] :
    # > # On Jun 27, 10:17 pm, Pe=F1a, Botp <> wrote:
    # > # > A little digression for everyone though...
    # > # > irb(main):013:0> [nil].sort
    # > # > =3D> [nil]
    # > # > is that intended?
    # > # ...why wouldn't it be? Sorting an array of any 1 element should =20
    # > 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 =20
    # 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
    Peña, Botp, Jun 29, 2007
    #13
  14. Cool Wong

    Peña, Botp Guest

    On Behalf Of :
    # 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 =3D=3D=3D 1
    =3D> true
    irb(main):043:0> 1 =3D=3D 1
    =3D> true
    irb(main):044:0> 1 > 1
    =3D> false
    irb(main):045:0> 1 < 1
    =3D> false
    irb(main):046:0> nil =3D=3D=3D nil
    =3D> true
    irb(main):047:0> nil =3D=3D nil
    =3D> 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
    Peña, Botp, Jun 29, 2007
    #14
  15. Cool Wong

    Guest

    ---2049402039-2137031982-1183114646=:6116
    Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-2137031982-1183114646=:6116"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    ---2049402039-2137031982-1183114646=:6116
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    Hi --

    On Fri, 29 Jun 2007, Pe=F1a, Botp wrote:

    > On Behalf Of :
    > # 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 re=

    turned.

    But it's a question of whether <=3D> is defined or not, so C.new is in
    exactly the same position as nil. So the question is: if [C.new]
    (one-element array) "sorts", why should [nil] not "sort"?


    David

    --=20
    * 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)
    ---2049402039-2137031982-1183114646=:6116--
    ---2049402039-2137031982-1183114646=:6116--
    , Jun 29, 2007
    #15
  16. Cool Wong

    F. Senault Guest

    Le 28 juin à 12:43, Peña, Botp a écrit :

    > # 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
    --
    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)
    F. Senault, Jun 29, 2007
    #16
  17. Cool Wong

    Peña, Botp Guest

    On Behalf Of
    #But it's a question of whether <=3D> is defined or not, so C.new is in
    #exactly the same position as nil. So the question is: if [C.new]
    #(one-element array) "sorts", why should [nil] not "sort"?

    yes, single elements do return _regardless_. i was used to thinking =
    nil-filled arrays wont sort directly. pls forgive my noise.

    in general,
    [anyobject].sort always return [anyobject]
    but
    [anyobject,anyobject].sort may or may not be defined (unless filtered =
    by block)

    thanks to everyone for the enlightenment.
    kind regards -botp

    ps: this may be for a new thread, and i should have asked this a long =
    time ago, but: why no <=3D> op for nil? so [nil, nil, nil].sort =3D> =
    [nil, nil, nil]
    Peña, Botp, Jun 30, 2007
    #17
  18. Cool Wong

    Peña, Botp Guest

    From: F. Senault [mailto:] :
    # 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 :
    indeed, thanks senault for the enlightenment. i was used to thinking =
    that if my sorting routines passes, then i don't have nil objects in =
    them. i totally missed the sorting algorithm for just one and only one =
    element.
    see my previous msg to david too.
    kind regards -botp
    Peña, Botp, Jun 30, 2007
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    0
    Views:
    522
    nobody
    Jun 1, 2004
  2. rkk
    Replies:
    9
    Views:
    797
    CBFalconer
    Sep 24, 2006
  3. Navin
    Replies:
    1
    Views:
    661
    Ken Schaefer
    Sep 9, 2003
  4. GIMME
    Replies:
    5
    Views:
    176
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
  5. Domenico Discepola

    multi-field array sort using Sort::Fields method

    Domenico Discepola, Apr 27, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    288
    Uri Guttman
    Apr 28, 2004
Loading...

Share This Page