Problem with sort!

V

Vance A Heron

Hello,
I'm having a problem using sort! to sort an array
in which each element is an array. I tried to generate
a short example, and am getting different, but still
puzzling behavior. In my real program, the error I get
is:
bin/newr2r.rb:107:in `sort': comparison of Array with Array failed
(ArgumentError)

Any help understanding this would be appreciated.
Thanks,
Vance

Short example:
---
#! /usr/bin/env ruby

a = [ ["Car", "Drive"], ["Boat", "Sail"], ["Plane", "Fly"] ]

puts "Original Array"
a.each{|v| p v}

puts "\nSort to new array"
b = a.sort{ |a, b| a[0] <=> b[0] }
b.each{|v| puts "class(#{v.class}) #{v[0]}, #{v[1]}"}

puts "\nSort to self"
a.sort!{ |a, b| a[0] <=> b[0] }
a.each{|v| puts "class(#{v.class}) #{v[0]}, #{v[1]}"}
---
Output from run...
SUN:quigon> sortprob.rb
Original Array
["Car", "Drive"]
["Boat", "Sail"]
["Plane", "Fly"]

Sort to new array
class(Array) Boat, Sail
class(Array) Car, Drive
class(Array) Plane, Fly

Sort to self
class(String) 67, 97
 
J

Jason Foreman

Hello,
I'm having a problem using sort! to sort an array
in which each element is an array. I tried to generate
a short example, and am getting different, but still
puzzling behavior. In my real program, the error I get
is:
bin/newr2r.rb:107:in `sort': comparison of Array with Array failed
(ArgumentError)
=20
Any help understanding this would be appreciated.
Thanks,
Vance
=20
Short example:
---
#! /usr/bin/env ruby
=20
a =3D [ ["Car", "Drive"], ["Boat", "Sail"], ["Plane", "Fly"] ]
=20
puts "Original Array"
a.each{|v| p v}
=20
puts "\nSort to new array"
b =3D a.sort{ |a, b| a[0] <=3D> b[0] }
b.each{|v| puts "class(#{v.class}) #{v[0]}, #{v[1]}"}
=20
puts "\nSort to self"
a.sort!{ |a, b| a[0] <=3D> b[0] }
a.each{|v| puts "class(#{v.class}) #{v[0]}, #{v[1]}"}
---

Don't use a as the variable name inside the block. You are
overwriting your original variable a when you do this.

a.sort {|x, y| ...} or something else...



Jason


Output from run...
SUN:quigon> sortprob.rb
Original Array
["Car", "Drive"]
["Boat", "Sail"]
["Plane", "Fly"]
=20
Sort to new array
class(Array) Boat, Sail
class(Array) Car, Drive
class(Array) Plane, Fly
=20
Sort to self
class(String) 67, 97
=20
=20
=20
 
D

David A. Black

Hi --

Hello,
I'm having a problem using sort! to sort an array
in which each element is an array. I tried to generate
a short example, and am getting different, but still
puzzling behavior. In my real program, the error I get
is:
bin/newr2r.rb:107:in `sort': comparison of Array with Array failed
(ArgumentError)

Any help understanding this would be appreciated.
Thanks,
Vance

Short example:
---
#! /usr/bin/env ruby

a = [ ["Car", "Drive"], ["Boat", "Sail"], ["Plane", "Fly"] ]

puts "Original Array"
a.each{|v| p v}

puts "\nSort to new array"
b = a.sort{ |a, b| a[0] <=> b[0] }
b.each{|v| puts "class(#{v.class}) #{v[0]}, #{v[1]}"}

puts "\nSort to self"
a.sort!{ |a, b| a[0] <=> b[0] }
a.each{|v| puts "class(#{v.class}) #{v[0]}, #{v[1]}"}

Don't use the same variable name (a) for your block variables. You're
trampling the array a. I think that's the root of your problems.


David
 
V

Vance A Heron

The real function was a little more complex than
could be done by sort by. I tried to generate
a smaller example, and confused myself.

I refactored a little to reduce duplicating
code - basically I have 2 separate sort methods,
depending on a value in the data - but they're
substantially the same. Anyway, by putting
the common code in a method and having sort
do something like vals.sort!{|a,b| sortfunc(a,b,sortflag) }
things seem to work.

Thanks to the list for all the help.

Hopefully my next problem/puzzle will be more
interesting.

V

Vance said:
puts "\nSort to new array"
b = a.sort{ |a, b| a[0] <=> b[0] }
b.each{|v| puts "class(#{v.class}) #{v[0]}, #{v[1]}"}

It's simpler with sort_by:

b = a.sort_by{ |x| x[0] }
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top