From: Stefan Rusterholz Date: 2007-11-14T00:56:26+09:00 Subject: Re: array ordering / selection according to elements - Shai Rosenfeld wrote: > > given that time1 < time2 < time3 < time4 ... (and are all Time objects) > > myarray= [ time1, time2, time3, time4, time5 , time6 , time7 , .. ] > > special_sort(time4)= [ time3, time4, time5 ] > > ...basically, passing an arg of a certain obj in the array to a > "special_sort" func, how do i collect the "nearest" ones to it? thx for > any suggestions min is O(n), gives you only the closest: array.min { |a,b| (closest_to - a).abs <=> (closest_to - b).abs } sort_by is O(nlogn), but you can use first(n) to get the n closest values: array.sort_by { |e| (closest_to - e).abs }.first(n) Regards Stefan -- Posted via http://www.ruby-forum.com/.