From: raja@... (Raja S.) Date: 2001-11-30T16:16:25+09:00 Subject: [ruby-talk:27036] Re: Help: factored sorting Bil Kleb writes: >I have an array of strings, >a = ["d3", "d33", "d4", "r16", "r33", "d34", "r34"] >which I'd like to sort by both the numerical suffix and by >the alphabetic prefix, i.e., I'd like the result, > ["d3", "d4", "d33", "d34", "r16", "r33", "r34"] >or, even better would be an array of two prefix-keyed >sub arrays that I could exploit with the assoc method, >viz, > [ ["d", "d3", "d4", "d33", "d34"], ["r", "r16", "r33", "r34"] ] Hmmm ... again. I seem to be going thru a think ... post ... re-think ... post cycle tonight. Rather than a single sorting phase followed by a grouping phase you could also have a grouping phase followed by individual sorts: Extending the 'gather' code posted earlier ... def sorted_groups( ary ) hsh = {} ary.each do |x| primary_key = keys(x).first if hsh.has_key?( primary_key ) hsh[primary_key] << x else hsh[primary_key] = [primary_key, x] end end hsh.keys.sort.collect{ |key| hsh[key].sort } end enough for now ... g'night Raja