From: "David A. Black" Date: 2008-03-29T15:51:52+09:00 Subject: Re: Help on best way to gather/sort results ? Hi -- On Sat, 29 Mar 2008, Tony De wrote: > Tony De wrote: >> Christian wrote: >>> Does the sort work if you just put in the sourceip? Say, you did >>> @results << >>> "#{sourceip}", and then used @results.sort. Does it still not sort? >>> >>> On Sat, Mar 29, 2008 at 4:14 PM, Tony De wrote: >>> >>>>>> >>>>> cigar, >>>> -- >>>> Posted via http://www.ruby-forum.com/. >>>> >>>> >>> >>> >>> -- >>> >>> "Every child has many wishes. Some include a wallet, two chicks and a >>> cigar, >>> but that's another story." >> >> Yeah, if I only collect the "sourceip" and do a @results.sort it doesn't >> work. I have to do a .sort!. And the same behaviour with @results << >> ["#{file}", "#{sourceip}", "#{email}", "#{subject}"]. @results.sort >> does not sort. @results.sort! does. And I tried sort! as an after >> thought. >> >> tonyd > > "as an after thought" got me to thinking. Try @results.sort! {|x, y| > y[1] <=> x[1]}. And it works. .sort fails, .sort! works. Any ideas > why? I would really like to understand this a little more. Thanks > guys, all of you, for your help. Just to be clear: they both work. They're different methods, though, and they do different things. sort! stores its results back in the original object; sort returns the results in a new object. The significance of the ! at the end is that the method is considered to be the "dangerous" version of the non-! method of the same name. (This is the conventional, intended meaning of !, although it has no language-level significance to the interpreter.) You can think of the "danger", in this case, as consisting of the fact that your original object will be altered. The ! is a kind of "heads up!" sign. David -- Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS April 14-17 New York City INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin See http://www.rubypal.com for details and updates!