C
Cool Wong
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]
Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]
List said:["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
A little digression for everyone though...
irb(main):013:0> [nil].sort
=> [nil]
is that intended?
Array = ["a", "a", "a", "b", "b", “câ€, "c", "c","d", "d", "e"]
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", "b", nil]["a", nil, "b"].sort_by { |e| (e.nil? ? 'z' : e) }
A little digression for everyone though...
irb(main):013:0> [nil].sort
=> [nil]
is that intended?
Let's do some philosophy!From: Phrogz [mailto:[email protected]] :
# > 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!From: Phrogz [mailto:[email protected]] :
# > irb(main):013:0> [nil].sort
# > =3D> [nil]
it gives the impression that nil elements are sortable.
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.
# > 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.
emselves are sortable. Remember, programs contain blackboxes wc may be comp=# > 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=
e because it's too dynamic and i want least number of surprises (especially=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=
turned.On Behalf Of (e-mail address removed):
# 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=
# 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.
=> [][].sort
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.