From: new2ruby@... (New 2 Ruby) Date: 2005-10-24T08:52:01+09:00 Subject: [SORT?] by x,y allowing x,y to be passed as parms Question: Are there better ways to do this sort within the container class? Have class CD with several fields defined, @list_of_CDs array in class CD_List holding CD pointers, and a couple of methods to sort array of pointers by any two fields. Below is relevant class entries and code. class CD attr_reader :size, :cusip, :issuer, :matures, ... .... class CD_List @@sortkey1= @@sortkey2 = "" def initialize @list_of_CDs = Array.new end ... def new_ord(key1='size',key2='matures') #Provide defaults @@sortkey1, @@sortkey2 = key1, key2 # Set class variables @list_of_CDs.sort! { |a,b| cmp_item(a,b) } # Sort in place end def cmp_item (a,b) # Adapted from Ryan Pavlik # Allow different sort fields via Class variables f1 = eval('a.'+@@sortkey1) <=> eval('b.'+@@sortkey1) f2 = eval('a.'+@@sortkey2) <=> eval('b.'+@@sortkey2) (f1 == 0 ? f2 : f1) end .... listCD = CD_List.new # Initialize it .... listCD.new_ord('matures','cusip') # Sort list of CDs __END__ Complete script with test data (143 lines) at: http://jmauney.freeshell.org/cgi-bin/data/CD_sort2.rb.txt Comments on entire script also solicited. Have searched this newsgroup, read FAQ at RubyCentral, read HTML of Pickax ver. 1, and Googled without success. Newest Pickax on order. Using ruby 1.8.2 (2004-12-25) [i386-mswin32] Email address is _invalid_! Read here; please respond here.