From: Brian Candler Date: 2009-03-11T21:29:50+09:00 Subject: Re: sorting question Miroslaw Niegowski wrote: > Is there a better way to do this ? I'm mostly concerned with memory > consumption... If this is the major concern, then you need to do an external sort: that is, one which doesn't load the whole file into memory. If you're on Unix, the 'sort' command will do this very nicely. All you need to do is to reformat it suitably (one pass), send it to 'sort' (which will do all the necessary spooling and merging), and then reformat it again (one pass). I'd suggest using the '-z' option to sort, so that each record is null-terminated. The way your records are structured, they should naturally sort in the correct order. Also set env['LC_ALL']='C' to be on the safe side. I've found strange problems with sort if this isn't done. If you want to implement an external sort in Ruby it's an interesting learning exercise, but is likely to be nowhere near as good as the Unix one. You could consider implementing a merge sort: e.g. split your file into 16 chunks, sort each one, then merge the 16 together one record at a time. -- Posted via http://www.ruby-forum.com/.