I have no experience creating and sorting multidimensional arrays in Ruby.
So I’m looking for general advice applicable to Sketchup objects.
Example: User has selected 5 component instances A,B,C,D,E.
I first derive n attributes of each: name, X, Y and Z of their origins, width, length and height etc.
So I have a table with 5 rows and n columns.
How can I sort the table by the values in any column?
This would be a Ruby feature not a SketchUp API feature.
Read up on the block forms of methods such as min, max, and sort.
You will need to write a method that takes the target column as an argument, then likely uses that to control a implementation of a sort block.
At the most basic, such a method would look similar to this:
(where i is the 0
based column index)
def sort_my_records(i)
@records.sort! {|a,b|
a[i] <=> b[i]
}
end
However, the <=>
operator requires the objects referenced to be Comparable
, ie if you test their class’s ancestors, the Comparable
module will be listed in the result, as one of the class’s ancestors,…
OR they have a <=>
method defined for their class.
String should not be a problem, and if your get the x,y and z from Point3d
objects, they will return Length
objects. Likewise for the h, w & d from BoundingBox
.
Length has a special <=>
override method defined.
http://www.sketchup.com/intl/en/developer/docs/ourdoc/length.php#<=>
Also ref: [url]Class: Array (Ruby 2.0.0)
And min and max are in the Enumerable module, which is mixed into Hash and Array, etc.
[url]http://ruby-doc.org/core-2.0.0/Enumerable.html[/url]