From: nobu.nokada@... Date: 2001-12-30T11:24:57+09:00 Subject: [ruby-talk:29687] Re: How to sort " sort -k3,3 -k9,9 file" At Sun, 30 Dec 2001 08:37:30 +0900, desibtis@hotmail.com (Sang DeSibtis) wrote: > > > In 1.6: > > > array.collect {|s| [s[2], s[8], s]}.sort.collect {|s| s.last} > > > > I did just like what you suggested and I got two numbers (int) followed by > the s (array item) when I used puts to look at the result. Do I have to add > any require statement anywhere in the 'rb' file to get it to work correctly? Sorry that I missed what you wanted. Since -k option to sort works wordwise, it should be: array.collect {|s| s.split.indexes(2, 8) << s}.sort.collect {|s| s.last} $ cat data 0 0 1 2 3 2 3 4 5 5 6 1 6 8 3 3 1 3 5 4 6 5 8 7 3 4 2 4 4 5 6 8 7 3 7 6 7 3 7 5 4 2 6 8 1 5 0 4 6 4 6 7 3 4 2 5 6 7 6 7 8 7 6 4 4 4 0 7 6 5 7 6 7 8 7 5 6 8 9 7 8 9 2 1 6 3 2 1 4 2 3 4 7 3 7 8 8 9 6 $ ruby -e 'puts ARGF.collect {|s| s.split.indexes(2, 8) << s}.sort.collect {|s| s.last}' data 1 5 0 4 6 4 6 7 3 4 2 0 0 1 2 3 2 3 4 5 5 6 8 7 3 4 2 4 4 5 6 8 7 4 2 3 4 7 3 7 8 8 9 6 3 7 6 7 3 7 5 4 2 6 8 0 7 6 5 7 6 7 8 7 5 6 8 9 7 8 9 2 1 6 3 2 1 5 6 7 6 7 8 7 6 4 4 4 1 6 8 3 3 1 3 5 4 6 5 $ ^ ^ > > > In 1.7: > > > array.sort_by {|s| [s[2], s[8]]} Also: array.sort_by {|s| s.split.select(2, 8)} > Is there an ftp site I can just download it? I don't know how to use that > CVS stuff. ftp://ftp.ruby-lang.org/pub/ruby/snapshot.tar.gz -- Nobu Nakada