Project

General

Profile

Actions

Bug #7182

closed

bug with Array#sort ?

Added by nazomikan (中島 拓哉) about 12 years ago. Updated about 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
ruby -v:
1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]
Backport:
[ruby-dev:46228]

Description

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"]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0