From: masta Blasta Date: 2013-06-19T02:46:36+09:00 Subject: Re: Compare and sort one array according to another. Robert Klemme wrote in post #1112735: > On Mon, Jun 17, 2013 at 11:35 PM, Douglas Seifert > wrote: > >> You have said that the size of your two arrays is small, so the fact that >> the find_index based solution is o(n^2) probably doesn't matter. >> > > Yes, that's what I was figuring. Your analysis is spot on. > > >> For kicks, a o(n) solution compared to the o(n^2) solution here: >> >> https://gist.github.com/seifertd/5800639 >> >> It involves precomputing a lookup index for the data you need to sort. >> > > Your "gen_lookup:" is exactly what I'd did for larger arrays. > > Kind regards > > robert This has been a great little learning exercise for me. I had never quite gotten how the sort_by method worked, and now reading it over again with the example it's perfectly clear. I'm surprised at how slow the method is. In the benchmark, 1.24s for 10 records...?? That's huge! The SQL query to fetch the records already sorted, as big and messy as it is, still only takes 10ms. I might have to stick with SQL on this one since for a Rails app response time is critical. I'm certainly glad I came and asked for help about this. -- Posted via http://www.ruby-forum.com/.