class Thing attr_accessor :location def initialize(x,y) @location = Location.new(x,y) end end class Location attr_accessor :x, :y def initialize(x, y) @x = x @y = y end end rnd = Random.new things = [*0..10].map { |x| Thing.new(rnd.rand(100), rnd.rand(100)) } puts things.inspect sorted_things = things.each.sort { |a, b| a.location.x <=> b.location.x } puts sorted_things.inspect Sample output: [#>, #>, #>, #>, #>, #>, #>, #>, #>, #>, #>] [#>, #>, #>, #>, #>, #>, #>, #>, #>, #>, #>]