T
Terje Tjervaag
Hi,
Just thought I'd write this to the list, since it wasn't immediately
obvious to me how to do it and I couldn't find it written anywhere on
the web..
Say you have an array of these music tracks:
Track =3D Struct.new("Track", :name, :artist, :album, :track_number)
..and you want to sort the array first by artist, then by album within
that artist and finally by track number within the album, this can
still be done by a pretty neat sort block:
sorted_tracks =3D tracks.sort do |a,b|
if a.artist !=3D b.artist
a.artist <=3D> b.artist
elsif a.album !=3D b.album
a.album <=3D> b.album
else
a.track_number.to_i <=3D> b.track_number.to_i
end
end
Just thought I'd write this to the list, since it wasn't immediately
obvious to me how to do it and I couldn't find it written anywhere on
the web..
Say you have an array of these music tracks:
Track =3D Struct.new("Track", :name, :artist, :album, :track_number)
..and you want to sort the array first by artist, then by album within
that artist and finally by track number within the album, this can
still be done by a pretty neat sort block:
sorted_tracks =3D tracks.sort do |a,b|
if a.artist !=3D b.artist
a.artist <=3D> b.artist
elsif a.album !=3D b.album
a.album <=3D> b.album
else
a.track_number.to_i <=3D> b.track_number.to_i
end
end