From: Terje Tjervaag Date: 2005-10-26T21:00:13+09:00 Subject: Re: sort on multiple attributes ------=_Part_2244_23842669.1130328010658 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 10/26/05, Robert Klemme wrote: > Btw, IMHO you should not need track_number.to_i as they should really be > stored as numbers. I agree - in this case I was playing around with making the most minimal iTunes Music Library parser I could, hence my use of .to_i. The current 14 line program is attached, if anyone is curious. It could be 9 if it wasn't for the error checking. > Alternative > > tracks.sort do |a,b| > [:artist, :album, :track_number].each do |field| > c =3D a.send(field) <=3D> b.send(field) > return c unless c =3D=3D 0 > end > 0 > end > > > I have also a related RCR pending that still can make it into the std > lib... > http://rcrchive.net/rcr/show/293 > I could see those additions being useful - it is a case of where to hide the inevitable complexity I suppose, and I think your RCR deals with it in a way that's very easy to read in the end. -- Terje ------=_Part_2244_23842669.1130328010658 Content-Type: application/octet-stream; name=itunes.rb Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="itunes.rb" require 'rexml/document' if !ARGV[0] then puts "Give me the path to the iTunes Music Library.xml file"; exit end Track = Struct.new("Track", :artist, :album, :track_number, :name, :location, :genre, :track_count) tracks = [] begin REXML::Document.new(File.new(ARGV[0])).elements.each("plist/dict/dict/dict") do |e| t = Track.new e.elements.each("key"){|e|m=e.text.downcase.gsub(" ","_")+"=";t.send(m,e.next_sibling.text) if t.respond_to?(m)} tracks << t end rescue REXML::ParseException puts "iTunes Library file format not recognised" end tracks.sort_by{|t|[t.artist,t.album,t.track_number.to_i]}.each{|track| puts track.values[0, 4].join(" - ")} ------=_Part_2244_23842669.1130328010658--