Cameron McBride
3/9/2006 4:18:00 PM
On 3/9/06, Toby DiPasquale <toby@cbcg.net> wrote:
> Speaking of this very topic, I am having an issue where I want to sort
> objects by two of its attributes in a SortedSet, but its not working
> out:
>
> I'd like it to be sorted first by its :a attribute, but if they are
> equal, by whatever sorting on the :b attributes comes up with.
as a quick alternative, sorting by several attributes is pretty easy
using #sort_by
X = Struct.new :a, :b
s = []
s << X.new(3, 3)
s << X.new(3, 4)
s << X.new(1, 2)
s << X.new(4, 8)
s << X.new(4, 2)
sorted = s.sort_by { |a| [a.a,a.b] }
sorted.each { |v| p v }
#<struct X a=1, b=2>
#<struct X a=3, b=3>
#<struct X a=3, b=4>
#<struct X a=4, b=2>
#<struct X a=4, b=8>
Cameron