From: Jenny Purcell Date: 2007-07-22T12:59:58+09:00 Subject: Re: Method to return elements of an array in numeric 'rank'? On Sun, 22 Jul 2007 11:44:40 +0900, James Britt wrote: >Jenny Purcell wrote: > >> I would expect there would be an existing method that works off of the >> array class that would do this, and I'd like to check on that before >> (painfully) writing my own nested loops that would do the same thing. > >See http://www.ruby-doc.org/core/classes/Enumerable.html#M003151 > >Look at sort_by > > >Also, in your code, consider representing objects of interest (such as >horses and races) by defining suitable classes. > >I think you code would be easier to follow if, for example, it managed a > collection of Horse objects. I realize it would be easier for people in the fgroup to follow if I was programming with objects. However, I just don't understand OO programming, my skills (such as they are) are procedural. Ruby was pointed out to me as a language that would allow me to write procedural programs and so far it is working out very nicely in that regard. Your suggestion to try .sort_by didn't work for me. However, I did read up on .each and that in fact DID work for me. The bit where I multiply my c variable by 100 and then add 20 (and later subtract 20) is a simple way for me to break ties. # Determining each horse's finishing position temp_score = Array.new(count + 1) {|c| 100 * score[c] + 20 - pole_new[c]} sort_score = temp_score.sort sort_score.reverse! number.each {|a| position[a - 1] = number.detect {|b| sort_score[b - 1] == temp_score[a - 1]}} Jenny