[ruby-trunk - Bug #7182][Open] bug with Array#sort?

Issue #7182 has been reported by nazomikan (中島 拓哉).


Bug #7182: bug with Array#sort ?
https://bugs.ruby-lang.org/issues/7182

Author: nazomikan (中島 拓哉)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.2
ruby -v: 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Array.sortにコードブロックを与え、出力した際( p ary.sort do |a,b| … end
)にコードブロックでのソート条件が無視されて出力される

case 1

ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 2
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 3
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort! do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 4
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary.sort! do |a, b|
a.to_i <=> b.to_i
end
p ary

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 5
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary2 = ary.sort do |a, b|
a.to_i <=> b.to_i
end
p ary2

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 6
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

Issue #7182 has been updated by nazomikan (中島 拓哉).

case 2,3,6は[“1”, “2”, “3”, “4”, “10”, “11”, “23”]が期待値だと思うのですがどうでしょう。

Bug #7182: bug with Array#sort ?
https://bugs.ruby-lang.org/issues/7182#change-31006

Author: nazomikan (中島 拓哉)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.2
ruby -v: 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Array.sortにコードブロックを与え、出力した際( p ary.sort do |a,b| … end
)にコードブロックでのソート条件が無視されて出力される

case 1

ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 2
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 3
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort! do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 4
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary.sort! do |a, b|
a.to_i <=> b.to_i
end
p ary

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 5
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary2 = ary.sort do |a, b|
a.to_i <=> b.to_i
end
p ary2

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 6
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

Issue #7182 has been updated by nazomikan (中島 拓哉).

do…endの優先度が低くてpの引数扱いされてるんですね。
失礼いたしました。 bugではありませんでした。


Bug #7182: bug with Array#sort ?
https://bugs.ruby-lang.org/issues/7182#change-31010

Author: nazomikan (中島 拓哉)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.2
ruby -v: 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Array.sortにコードブロックを与え、出力した際( p ary.sort do |a,b| … end
)にコードブロックでのソート条件が無視されて出力される

case 1

ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 2
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 3
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort! do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 4
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary.sort! do |a, b|
a.to_i <=> b.to_i
end
p ary

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 5
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary2 = ary.sort do |a, b|
a.to_i <=> b.to_i
end
p ary2

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 6
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

Issue #7182 has been updated by nazomikan (中島 拓哉).

ご説明ありがとうございます。
非常に勉強になりました、お騒がせしてしまい申し訳ございません。


Bug #7182: bug with Array#sort ?
https://bugs.ruby-lang.org/issues/7182#change-31012

Author: nazomikan (中島 拓哉)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.2
ruby -v: 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Array.sortにコードブロックを与え、出力した際( p ary.sort do |a,b| … end
)にコードブロックでのソート条件が無視されて出力される

case 1

ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 2
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 3
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort! do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 4
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary.sort! do |a, b|
a.to_i <=> b.to_i
end
p ary

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 5
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary2 = ary.sort do |a, b|
a.to_i <=> b.to_i
end
p ary2

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 6
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

Issue #7182 has been updated by no6v (Nobuhiro IMAI).

case 2,3,6は[“1”, “2”, “3”, “4”, “10”, “11”, “23”]が期待値だと思うのですがどうでしょう。

これは、

p(ary.sort) do |a, b|
a.to_i <=> b.to_i
end

と解釈されて、ブロックは p に渡したと解釈されて、無視されています。
() を使うか、ブロックを {} で書くか、一旦別の変数に代入すると、期待する通りに動くと思います。

p(ary.sort do |a, b|
a.to_i <=> b.to_i
end)

p ary.sort{|a, b|
a.to_i <=> b.to_i
}

ary2 = ary.sort do |a, b|
a.to_i <=> b.to_i
end
p ary2


Bug #7182: bug with Array#sort ?
https://bugs.ruby-lang.org/issues/7182#change-31011

Author: nazomikan (中島 拓哉)
Status: Open
Priority: Normal
Assignee:
Category: core
Target version: 1.9.2
ruby -v: 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Array.sortにコードブロックを与え、出力した際( p ary.sort do |a,b| … end
)にコードブロックでのソート条件が無視されて出力される

case 1

ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 2
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 3
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort! do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 4
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary.sort! do |a, b|
a.to_i <=> b.to_i
end
p ary

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 5
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary2 = ary.sort do |a, b|
a.to_i <=> b.to_i
end
p ary2

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 6
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

Issue #7182 has been updated by sorah (Shota F.).

Status changed from Open to Rejected


Bug #7182: bug with Array#sort ?
https://bugs.ruby-lang.org/issues/7182#change-31013

Author: nazomikan (中島 拓哉)
Status: Rejected
Priority: Normal
Assignee:
Category: core
Target version: 1.9.2
ruby -v: 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Array.sortにコードブロックを与え、出力した際( p ary.sort do |a,b| … end
)にコードブロックでのソート条件が無視されて出力される

case 1

ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 2
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 3
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort! do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

#case 4
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary.sort! do |a, b|
a.to_i <=> b.to_i
end
p ary

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 5
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
ary2 = ary.sort do |a, b|
a.to_i <=> b.to_i
end
p ary2

result: [“1”, “2”, “3”, “4”, “10”, “11”, “23”]

#case 6
ary = [“1”, “2”, “10”, “11”, “3”, “4”, “23”]
p ary.sort do |a, b|
a.to_i <=> b.to_i
end

result: [“1”, “10”, “11”, “2”, “23”, “3”, “4”]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs