From: Gunther Diemant Date: 2011-07-01T20:17:34+09:00 Subject: Re: How to order Structs based on two fields --0016364ec992f272f304a700293d Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Sorry code must be array.sort { |x,y| x.a =3D=3D y.a ? -(x.b <=3D> y.b) : x= .a <=3D> y.a } 2011/7/1 Gunther Diemant > Try this: array.sort { |x,y| x.a =3D=3D y.a ? x.b <=3D> y.b : x.a <=3D> y= .a } > Or overide the <=3D> method in your struct. > > > 2011/7/1 I=F1aki Baz Castillo > >> 2011/7/1 I=F1aki Baz Castillo : >> > I expected that the following could work: >> > >> > array.sort_by{|entry| entry.a or entry.b} >> > >> > but it generates: >> > >> > [kk1, kk2, kk3, kk4] >> > >> > so obviously it does not work. I'm not very used to Enumerable.sort_by >> > method, any tip please? >> >> Maybe it would be better to use "class KK" rather than an struct, and >> then define <=3D> method for KK class (and include Enumerable module)? >> >> -- >> I=F1aki Baz Castillo >> >> >> > --0016364ec992f272f304a700293d--